Search code examples
phpsearchpreg-replacestr-replacepreg-replace-callback

Removing HTML without escaping


<div class="item itemask" ><div class="tophead"><div class="itemnumber"></div><a class="article" href=""></a> </div><div class="bottomhead"> points by <a class="userlink" rel=""></a>&nbsp;&nbsp;  ago&nbsp;&nbsp;&nbsp;<a href="http://news.ycombinator.com/item?id=">discuss</a></div></div>

How can PHP remove all instances of the above snippet in a page without requiring escaping the above string. Or, how can I easily get the escape-string of the above in order to plug into a preg_replace to remove it.

I need to run PHP on the server against a bunch of htm files. It must be done on the server-side in one operation.


Solution

  • You could escape your search string with preg_quote:

    string preg_quote ( string $str [, string $delimiter = NULL ] )
    

    preg_quote() takes str and puts a backslash in front of every character that is part of the regular expression syntax. This is useful if you have a run-time string that you need to match in some text and the string may contain special regex characters.

    The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -