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 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败