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条)

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法