8

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 Rocha
  • 9,510
  • 18
  • 69
  • 121

2 Answers2

18

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
  • 54,546
  • 15
  • 119
  • 154
hjpotter92
  • 75,209
  • 33
  • 136
  • 171
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
  • 141,508
  • 20
  • 234
  • 284