dsxi70423 2011-08-10 15:26
浏览 116
已采纳

PHP / Smarty中输出开头的空格字符 - 如何跟踪它?

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="
"}
{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'.

  • 写回答

1条回答 默认 最新

  • dtiu94034 2011-08-10 16:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路