php regex escaping special characters

php, regex

Solution

Only the characters listed on this page need to be escaped in PHP regex matching/replacing.

While `<` and `>` can act as delimiter, it doesn't need to be escaped in the given example because you already have `/`(slash) acting as a delimiter.

Referring to the link in question

The `preg_quote()` function may be used to escape a string for injection into a pattern and its optional second parameter may be used to specify the delimiter to be escaped.

Problem

I wrote the following code (yes it does work) and was wondering why I don't need to escape the '<' and '>' characters inside the pattern since they are considered 'special' characters by the php manual. http://www.php.net/manual/en/function.preg-quote.php ``` var_dump(preg_match('/<[A-Za-z][A-Za-z0-9]*>/', "<html>", $matches)); echo "<pre>"; var_dump(htmlentities($matches[0])); echo "</pre>"; ``` output: ``` int(1) string(12) "<html>" ```

Original source

Related problems