【问题标题】:What is the best way to replace the punctuation in tag names?替换标签名称中的标点符号的最佳方法是什么?
【发布时间】: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 不是解析htmlxml 的正确方法,但我担心在我的情况下我很想使用preg_replace(),因为DOMDocument() 无法读取我的大约 250K 行结构错误的命名空间提供的 xml- 内容。提供的 xsd 文件(约 25 个方案)已过时(已有 6 年),内容提供者不愿修复此问题。

我发现SimpleXMLElement() 在将: 替换为_ 后可以工作。

【问题讨论】:

  • 是单串还是一串串?
  • 这是一个包含大约 225K 行内容的单个字符串;)
  • 为什么要这样做? :-)
  • 好的,我想我已经改进了我的问题,似乎还有更多需要注意的地方。

标签: php regex xml-parsing preg-replace


【解决方案1】:

您可以捕获&lt;&gt; 之间的内容,然后将: 替换为_,如下所示:

$string = "<data:data>
<another:data:data>Content:</another:data>
<another:data>:Content</another:data>
<another:data>Content</another:data>
<another:data><![CDATA[This content should : not be affected]]>Content</another:data>
</data:data>";

$regex = '~<[^!][^>]*>~';
$replaced = preg_replace_callback(
    $regex,
    function($m) { return str_replace(':', '_', $m[0]);},
    $string);

echo $replaced;

输出:

<data_data>                                                                                                                                                                                          
<another_data_data>Content:</another_data>                                                                                                                                                           
<another_data>:Content</another_data>                                                                                                                                                                
<another_data>Content</another_data>                                                                                                                                                                 
<another_data><![CDATA[This content should : not be affected]]>Content</another_data>                                                                                                                                                                   
</data_data>

【讨论】:

  • 我非常感谢你,我认为这接近我所需要的,我仍然坚持&lt;![]&gt; 之间的属性内容和CDATA 的内容。我已经改进了我的问题,你能看看吗?
  • &lt;!&gt;之间改变了满足
  • 已编辑答案,请检查
【解决方案2】:

如果您不使用属性,此代码将适用于您:

$string = preg_replace_callback(
    '#</?[\w:]+>#',
    function ($match) {
        list($tag) = $match;
        return str_replace(':', '_', $tag);
    },
    $string
);

如果您确实使用了属性,请查看:How do I change XML tag names with PHP?

【讨论】:

  • 该死的。我没有提到,但也有属性。感谢您为我提供这个答案,我将查看 preg_replace_callback()。
【解决方案3】:

你的意思是:

$string = "<data:data>
<another:data>Content</another:data>
<another:data>Content</another:data>
<another:data>Content</another:data>
<another:data>Content</another:data>
</data:data>";

$string = str_replace(':', '_', $string);

$string = str_replace('another:data', 'another_data', $string);

更新

也许您可以尝试以下方法:

$replace = array('another:data' => 'another_data', '/another:data' => '/another_data'); // So you can easily add more strings to replace
strtr($string, $replace);

链接:http://php.net/strtr。我刚找到这个,所以不知道这是否适合你。

【讨论】:

  • 这也会影响内容。
  • 你能告诉我吗? @Arek van Schaijk 怎么样?
  • 嗯,标签内的所有标点符号都被替换为。
  • @anantkumarsingh 如果内容中有类似The last name of John is: herp 的内容,那么: 将替换为_
  • 有数百个不同的标签,所以你的更新不会影响这一点,恐怕我已经附上了使用 preg_replace()。
【解决方案4】:

您可以尝试以下正则表达式,

<\/?\w+(:)\w+>

Working Demo

您可以使用Group捕获将其替换为_

【讨论】:

    猜你喜欢
    • 2011-06-13
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2011-01-22
    相关资源
    最近更新 更多