dongshi1606 2012-02-22 08:55
浏览 86
已采纳

PHP - XML到CSV删除XML空白

I'm trying to convert a XML file to CSV using PHP, but I have a problem with whitespaces for some values inside a tag. This is the XML:

<Product>
<Code>1234</Code>
<Name>product name</Name>
<Category>some category</Category>
<Manufacturer>manufacturer</Manufacturer>
<Price>                 100</Price>
<Stock>1</Stock>
</Product>

As you can see, there's a lot of whitespace inside the Price tag, which messes with my CSV formatting. I've tried this but it didn't work:

$Product->Price = preg_replace("/>\s+</", "><", $Product->Price);

Any help will be greatly appreciated. Thanks in advance.

  • 写回答

4条回答 默认 最新

  • dousui7410 2012-02-22 08:58
    关注

    Try trim(). From the php manual:

    This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:

    " " (ASCII 32 (0x20)), an ordinary space. "\t" (ASCII 9 (0x09)), a tab. " " (ASCII 10 (0x0A)), a new line (line feed). "" (ASCII 13 (0x0D)), a carriage return. "\0" (ASCII 0 (0x00)), the NUL-byte. "\x0B" (ASCII 11 (0x0B)), a vertical tab.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致