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

前端 未结 7 2095
栀梦
栀梦 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:

    
        Something
    Something Else

    Is that
    supposed to read <br/>? 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 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 ]]>.

提交回复
热议问题