I am generating and exporting a CSV through PHP and after some modifications from my team, now it results that inside a column, double quotation marks are being generated. I generate it through my terminal by executing this Shell script with the CakePHP Console.
/var/www/mysite.new/trunk/app/Console/cake Csv mysite.uk
The problem is that I already tried many techniques to strip them off such as: stripslashes(), str_replace(), trim()
On my last modification, I tried to apply the str_replace function.
foreach ($persons_csv as $person_csv){
/* The part where I get the data for stripping off the quotation marks */
$mail = $person_csv['Person']['email'];
$name = str_replace('"', '', $person_csv['Person']['name']);
$surname = str_replace('"', '', $person_csv['Person']['surname']);
/* REST OF THE CODE */
}
Nevertheless, it only happens to surnames and names that have more than one word in which the quotations marks are being generated. Surnames and names that are consisting of one word, they appear to be fine. Still, there are some anomalies probably inside names that have whitespace and therefore double quotations marks are being generated again. I am not quite sure why this is ocurring. I can attach you two screenshots so you can have a better understanding of the problem.
If you have any idea of what it might be, it would be really appreciating. This is the rest of my code in which I am generating the CSV.
private function addRow($row) {
$rows_deleted = 0;
if (!empty($row)){
fputcsv($this->buffer, $row, $this->delimiter, $this->enclosure);
} else {
return false;
}
}
private function renderHeaders() {
header("Content-type:application/vnd.ms-excel");
header("Content-disposition:attachment;filename=" . $this->filename);
}
private function setFilename($filename) {
$this->filename = $filename;
if (strtolower(substr($this->filename, -4)) != '.csv') {
$this->filename .= '.csv';
}
}
private function render($filename = true, $to_encoding = null, $from_encoding = "auto") {
if(PAIS) {
if ($filename) {
if (is_string($filename)) {
$this->setFilename($filename);
}
$this->renderHeaders();
}
rewind($this->buffer);
$output = stream_get_contents($this->buffer);
$url = '/var/www/mysite.new/trunk/' .'app'.DS.'webroot'.DS.'csv'.DS.PAIS.DS.$this->filename;
$gestor = fopen($url, "w+") or die("Unable to open file");
if(file_exists($url)){
file_put_contents($url, $output);
chmod($url, 0777);
fclose($gestor);
} else {
return false;
}
} else {
return false;
}
}
public function csv_persons($persons_csv) {
$this->array_final = [self::NAME, self::SURNAME];
date_default_timezone_get('Europe/Madrid');
$d = date("Ymd");
$this->addRow($this->array_final);
foreach ($persons_csv as $person_csv){
$name = str_replace('"', '', $person_csv['Person']['name']);
$surname = str_replace('"', '', $person_csv['Person']['surname']);
$apos = ''';
$pos = strpos($surname, $apos);
if($pos !== false) {
$surname = str_replace(''', '\'', $surname);
}
$arr = array();
$arr[$this->getArrayKeyIndex($this->array_final, self::NAME)] = $name;
$arr[$this->getArrayKeyIndex($this->array_final, self::SURNAME)] = $surname;
$this->addRow($arr);
}
$filename = 'PERSON_PROFILE_' . $d;
$this->render($filename);
}
Thanks
Instead of using fputcsv
, try implode
.
Ref: https://www.php.net/manual/en/function.implode.php
Update 1: You have to be sure that your value does not contain , (comma)
Update 2: If you are concern with the idea about that quoted text will be problem for your CSV datasheet, than you need to know that CSV is designed to that if there is any space between the value. So you don't have to worry about that. Any CSV parser will understand the quoted values properly.