This is my PHP
$val = "";
foreach($data as $v){
$val .= '"' . $v . "
" . '"';
}
The result is
//this is inside of cell
sample
"sample
"sample
This is correct but I cant figure out how to remove the double quote in "sample
. Only the first value dont have a double quote the rest will have a double quote.
EDIT
This is the result of my code
This is what I want
Somthing like this should do the trick
$i = 0
foreach($data as $v){
$val .= '"' . $v . "
" . (count($data) !== $i) ? '"' : '';
$i++;
}
I think you can use the php function trim
like this:
$val = "";
foreach($data as $v){
if( ''==$val) {
$val .= trim('"' . $v . "
" . '"','"');
}else{
$val .= '"' . $v . "
" . '"','"';
}
}
Just Remove some part of your code, you will get solution:
$val = "";
foreach($data as $v){
$val .= $v . "
";
}
Just try (Maintaining line break in excel):
$val = "";
foreach($data as $v){
$val .= $v . "<br style='mso-data-placement:same-cell;' />";
}
$val .= '"' . $v . "</br>" . '"';
$val=str_replace('"','',$val);
I think this is what you are looking for inside your loop. It will remove every double quote.
Well there you go :
$data = ['sample','sample','sample'];
$val = "";
foreach($data as $v){
$val .= $v . "
" ;
}
Then you simply use fputcsv() like this :
$fp = fopen('file.csv', 'w');
fputcsv($fp, [$val]);
And you get the result you want :
The answer to my question is
$val = "";
foreach($data as $v){
$val .= $v . "
";
}
$val = '"'. $val .'"';
I need to add the '"'
outside of the loop.