We need a way to remove HTML comments when displayed to the user but not to the developer. I'm wondering what the best way to do this is.
+1
A:
Try:
$html = eregi_replace("<!--[^>]*-->", "", $html);
Or using preg_replace, as mentioned below:
$html = preg_replace("/<!--(.|\s)*?-->/", "", $html);
And, yes, either way, comment delimiters embedded in strings will cause problems.
Andy
2009-09-30 09:05:03
What about `<p title="<!--">foo--></p>`?
Gumbo
2009-09-30 09:12:25
ereg is deprecated PHP regex functionality due for removal in PHP 6. Use preg_replace, it's faster anyway.
Evernoob
2009-09-30 09:41:07
+1
A:
The best would be to use a parser like DOMDocument, traverse the DOM tree and remove all comment nodes. Or you build a parser on your own.
Gumbo
2009-09-30 09:09:06
+1
A:
Are you removing comments from your own PHP pages, or from someone else's markup you have in a string?
For the latter, use an HTML parser, as suggested by Gumbo. Don't use regex to parse HTML, because it can't. (I need to bind a hotkey to that sentence. Every third question on SO is trying to parse HTML with a regex. It is not a good idea.)
If you mean the former, just use PHP comments instead of HTML ones and you won't have to do any post-processing:
<?php /* blah */ ?>
bobince
2009-09-30 09:54:41