PHP: No match when using exclamation mark in regular expression

1.2k Views Asked by At

I'm trying to use preg_match to find a commented part in a loaded HTML file. The exclamation mark present seems to break it, however. Whenever I try to use

preg_match("/<!/", "<!", $matches);

an empty $matches[0] is returned. I've tried

preg_match("/< !/", "< !", $matches);
preg_match("/!</", "!<", $matches);

and these do return "< !" and "!<". I've searched everywhere for a solution, but I can't seem to find a solution for this most likely very easy-to-solve problem.

Thanks in advance.

1

There are 1 best solutions below

3
On BEST ANSWER

Are you sure an empty $matches[0] is returned, or is it just that $matches[0] is itself an HTML comment that gets hidden? Try var_dump($matches) and check the length of the string.