Re: Re: strip comments from HTML?
| From: | Rob Ellis | Date: | Thu, 06 May 2004 15:26:34 +0000 |
| Subject: | Re: Re: strip comments from HTML? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-185532@lists.php.net to get a copy of this message | ||
On Thu, May 06, 2004 at 11:10:17AM -0400, Paul Chvostek wrote:
> On Thu, May 06, 2004 at 03:02:16PM +1000, Justin French wrote:
> >
> > This isn't working:
> > $text = preg_replace('/<!--(.*)-->/','',$text);
> >
> > Can someone advise what characters I need to escape, or whatever to get
> > it going?
>
> It's not a matter of escaping. You're matching too much with the ".*".
>
> If you're sure you won't have any right-point-brackets inside comments,
> you can use something like:
>
> $text = ereg_replace("<!--[^>]*-->","",$text);
>
> Accurately matching comments in an extended regular expression is tricky
> though. The only thing you can really *negate* in an ereg is a range,
> not an atom. And the close of the comment can't be prepresented as a
> range, since it's multiple characters.
>
> Not to say it can't be done. I just can't think of how at the moment.
>
you can make the .* less greedy...
$text = preg_replace('/<!--.*?-->/', '', $text);
- rob