I am dealing with a rather large web app in PHP with smarty. One part of it requires spitting out a report in CSV format. I set all required headers and pass data to the smarty template:
$template->assign('headers', $report->get_headers());
$template->assign('data', $report->get_data());
My template looks like this:
{strip}
{assign var="newline" value="\n"}
{foreach from=$headers item=h name=hdr}
"{$h|replace:'"':'""'}"
{if !$smarty.foreach.hdr.last},{/if}
{/foreach}
{$newline}
{if trim($warntext) != ''}
"{$warntext|replace:'"':'""'}"
{$newline}
{/if}
{foreach from=$data item=row name=res}
{foreach from=$row item=v name=val}
"{$v|replace:'"':'""'}"
{if !$smarty.foreach.val.last},{/if}
{/foreach}
{if !$smarty.foreach.res.last}{$newline}{/if}
{/foreach}
{/strip}
Everything works fine - except one problem. Somewhere there's one space character being printed in the very beginning of the output, so the first row in the CSV (headers) looks like this:
[space-char]"ID","Name","Reg date",...
As a result, Excel messes up the first heading: A1 cell contains ' "ID"'
(without the single quotes, of course). Obviously, there's something somewhere printing this space char - but how can I find it? I tried tracing the execution of the report (there are dozens of php files being required
from inside each other - with a total of more than 10K lines of code to get the report). I tried searching for echo
and print
and print_r
- but nothing. I'm getting desperate now. Any help would be greatly appreciated.
P.S. Please do not comment/answer with "let them just fix the output file", as these are 开发者_如何转开发external clients I'm dealing with: I can't tell them to 'please edit the file before opening in Excel'.
Try looking for whitespaces right before opening <?php
tags and right after closing ?>
tags (if used at all). These are common places to have spaces accidently.
精彩评论