1

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.

Thevet
  • 154
  • 9
  • Cannot reproduce: Works fine in CLI. However, you should _always_ escape the escape symbol, if you don't want it's escaping behaviour `\\\` – KingCrunch Apr 06 '12 at 23:17
  • Very odd. I wonder what could be it. That was actually a typo. Haha. – Thevet Apr 06 '12 at 23:21

1 Answers1

1

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.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Same thing. This is returned: array(1) { [0]=> string(2) " – Thevet Apr 06 '12 at 23:20
  • See, the length is 2. What's happening is that `<!` is seen as the start of an HTML tag, and so is not shown in the browser. If you do `echo htmlspecialchars($matches[0]);` you will very clearly see `<!`. You would also see `<!` if you viewed the source of the page. – Niet the Dark Absol Apr 06 '12 at 23:22
  • Oh, that makes sense. I apologise for my stupidity. Haha. Thank you very much. – Thevet Apr 06 '12 at 23:24