Re: Re: strip comments from HTML?

From: Date: Thu, 06 May 2004 17:39:50 +0000
Subject: Re: Re: strip comments from HTML?
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-185545@lists.php.net to get a copy of this message
On Thu, May 06, 2004 at 12:47:10PM -0400, Paul Chvostek wrote: > On Thu, May 06, 2004 at 11:26:34AM -0400, Rob Ellis wrote: > > > > > > $text = ereg_replace("<!--[^>]*-->","",$text); > > > > you can make the .* less greedy... > > > > $text = preg_replace('/<!--.*?-->/', '', $text); > > Interesting to know. My preg-foo is limited; I came at PHP from a > background of awk and sed, so when I regexp, I'm a little more > traditional about it. > > Interestingly, from a shell: > > $ text='one <!-- bleh --> two\nthree <!-- blarg -->four\n' > $ printf "$text" | sed -E 's/<!--([^-][^-]?[^>]?)*-->//g' > one two > three four > > which is the same behaviour as PHP. But that still doesn't cover > multi-line. PHP's ereg support is supposed to, but doesn't work with > this particular substitution: > > $text="one <!--bleh\nblarg -> two\n"; > print ereg_replace("<!--([^-][^-]?[^>]?)*-->", "",$text); > > returns > > one <!--bleh > blarg -> two > > But we know it really does support multiline, because: > > $text="aaaabb\nbbcccc"; > print ereg_replace("[^ac]","",$text); > > returns > > aaaacccc > > So ... this is interesting, and perhaps I'll investigate it further if > the spirit moves me. ;-) right, to strip multi-line comments with preg_replace you need /s $text = preg_replace('/<!--.*?-->/s', '', $text); - rob

« previous php.general (#185545) next »