I\'m trying to write a regular expression using the PCRE library in PHP.
I need a regex to match only &
, >
and <
cha
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 <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 <![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 ]]>
.