Regular expression to match “>”, “<”, “&” chars that appear inside XML nodes

前端 未结 7 2096
栀梦
栀梦 2020-12-19 02:42

I\'m trying to write a regular expression using the PCRE library in PHP.

I need a regex to match only &, > and < cha

相关标签:
7条回答
  • 2020-12-19 03:08

    As stated by others, regular expressions don't do well with hierarchical data. Besides, if the data is improperly formatted, you can't guarantee that you'll get it right. Consider:

    <xml>
        <tag>Something<br/>Something Else</tag>
    </xml>
    

    Is that <br/> supposed to read &lt;br/&gt;? There's no way to know because it's validly formatted XML.

    If you have arbitrary data that you wish to include in your XML tree, consider using a <![CDATA[ ... ]]> block instead. It's treated the same as a text node, and the only thing you don't have to escape is the character sequence ]]>.

    0 讨论(0)
提交回复
热议问题