dongyouji7022 2014-09-18 22:14
浏览 22
已采纳

too long

This question already has an answer here:

I'm using simplexml_load_string to load an XML document into an object. This seemed to be working great up until I came across this element:

<some_string_val>1.&#160;&#160;&#160;&#160; Some text.</some_string_val>

After running that ran through simplexml_load_string, what came out was:

["some_string_val"]=> string(20) "1.     Some text"

I tried using:

html_entity_decode($string,  ENT_QUOTES, "Windows-1252");

And that seemed to convert the &#160;'s to plain text, but when I tried to run that through simplexml_load_string I get the same result. I also tried with UTF-8, and a few others, with similar or worse results.

So, what can I do to convert the &#160;'s to UTF-8 so it can be parsed correctly by simplexml_load_string? Keeping the HTML entities intact is not a concern because this is going into a CSV.

EDIT: This has been unjustly marked as a duplicate for a couple of reasons:

  1. This is not language agnostic; this is dealing with a specific set of PHP functions, unlike the post which this was marked a duplicate of
  2. This is not going to an HTML page or a PDF, it is going to a CSV, so I cannot set a header. The accepted solution will not work in my case
</div>
  • 写回答

2条回答 默认 最新

  • drm16022 2014-09-18 22:30
    关注

    I think it parses correctly. It just the way that function works, replacing those codes with special characters.

    You can fix the result string, converting it into cp1251

    $str = iconv('utf-8', 'cp1251', $str);
    

    Also I would delete double spaces before writing it into CSV file

    $str = str_replace(chr(160), ' ', $str);
    $str= trim(preg_replace('/\s+/', ' ', $str));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法