11

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>"
Robert
  • 10,126
  • 19
  • 78
  • 130

2 Answers2

21

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.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
3

< and > aren't meta characters is most contexts.

However they are used as such for:

  • named capture groups (?P<name>)
  • lookbehind assertions (?<=...)

So that's why preg_quote plays it safe and escapes them. It's arguably redundant, since escaping ( and ? would be sufficient. But it doesn't hurt either.

mario
  • 144,265
  • 20
  • 237
  • 291