【发布时间】:2015-06-10 18:36:12
【问题描述】:
我有理由将所有标签名称中的标点符号替换为下划线(请不要问我为什么它与问题无关)。
与问题相关的是我想:
<data:data>
<another:data>Content</another:data>
<another:data>Content</another:data>
<another:data>Content</another:data>
<another:data attribute="attr : content">This content should : not be affected</another:data>
<another:data><![CDATA[This content should : not be affected]]></another:data>
</data:data>
替换为:
<data_data>
<another_data>Content</another_data>
<another_data>Content</another_data>
<another_data attribute="attr : content">This content should : not be affected</another_data>
<another_data><![CDATA[This content should : not be affected]]></another_data>
</data_data>
但是使用php 执行此操作的最佳方法是什么?
我知道regex 不是解析html 或xml 的正确方法,但我担心在我的情况下我很想使用preg_replace(),因为DOMDocument() 无法读取我的大约 250K 行结构错误的命名空间提供的 xml- 内容。提供的 xsd 文件(约 25 个方案)已过时(已有 6 年),内容提供者不愿修复此问题。
我发现SimpleXMLElement() 在将: 替换为_ 后可以工作。
【问题讨论】:
-
是单串还是一串串?
-
这是一个包含大约 225K 行内容的单个字符串;)
-
为什么要这样做? :-)
-
好的,我想我已经改进了我的问题,似乎还有更多需要注意的地方。
标签: php regex xml-parsing preg-replace