I am having a text area in my UI and I would like to filter out all xml(including html) text entered by user using preg_replace. I need a some help with it.
Eg:
Emailed user sam at 12:15 PM
<details><email>sam@sam.com</email></details>
After preg_replace :
Emailed user sam at 12:15 PM
Along with this any pointers to preg_replace regex would be great. I tried using some java regex(I am new to PHP) in preg_replace and found that it did not work. I am not sure if my regex was wrong or preg_replace uses different kind of regex.
All your help is much appreciated.
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )So something like the following will work in most cases,
Where
'/<[\/]{0,1}\w[^>]*>/g'is regex for <name stuff> and </name stuff>. It will fail if you’ve a > in an attribute (i.e.<name blah=">hello">will result inhello">Although
strip_tagsis native so will be faster. Remember the arguments forstrip_tagsare the things to not stripEDIT: If you want the content between the tags stripped out too, the regex for that is
'/<\w[^>]*>[\s\S]*?<\/\w[^>]*>/g', but this will fail in even more situations (i.e. badly closed/nested tags), so you might want to run it first then the above on the result to make sure.EDIT: Spelling