I am generating a csv from an array of arrays using fputcsv() . One of the field contians a html code snippet and it is causing problems when I open it in Excel.
When opened in excel , it breaks across multiple cells .
Please note that in the fputcsv I have used ,
as the delimiter and "
as the enclosing element.
Still I could see that the html string was being broken across multiple cells , and this was occuring because of commas inside the htmlcontent .
So , how can I exactly escape the inlying commas ?
For the time being , I did replace the commas with whitespace . But , still I am seeing that a long htmlcode (nearabout 53823 characters) breaks across multiple cells when opened in MS Excel . Is there any upper limit to the number of characters that a single cell in Excel can contain?
Please note that when I open my csv in Notepad++ , it shows that the CSV is well formed without any breaks in between. However , my client uses MS Excel , and it's kind of hard convincing him that the CSV is actually well formed . Please help.
Here's the code that I have been using :
//escape the newline characters
$template = str_replace(array("\r\n", "\n", "\r", "\t"), '', $template);
$cache = str_replace(array("\r\n", "\n", "\r", "\t"), '', $cache);
$fh = fopen('abc.csv', 'a');
fputcsv($fh,array($template,$cache),',','"');
the $template
and $cache
are having html code snippet as field values.
Also , what I noticed is that if I don't escape the ,
, the string breaks across multiple cells . For this I have tried to str_replace(",","\,",$string)
but to no use.
Update : The issue is in cases where both quotes and comma occur inside a string .
e.g consider a string I have "big" quesiton, that I want to know.
"I have "big" quesiton"
comes in one cell and "that I want to know."
comes in another cell instead of the entire string occuring in one column.