RE: [PHP] Re: strip comments from HTML?
| From: | Michael Sims | Date: | Thu, 06 May 2004 16:21:40 +0000 |
| Subject: | RE: [PHP] Re: strip comments from HTML? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-185539@lists.php.net to get a copy of this message | ||
David T-G wrote:
> % you can make the .* less greedy...
> %
> % $text = preg_replace('/<!--.*?-->/', '', $text);
>
> How does that make it ungreedy? The only thing I see relating
> question marks and greediness is that a question mark makes part of a
> regexp with a /U modifier (otherwise not greedy) back into a greedy
> portion.
>
> Am I missing something painfully obvious?
www.perldoc.com appears to be unavailable at the moment, but if you have "perldoc"
installed, here's an excerpt from the "perlre" man page:
By default, a quantified subpattern is "greedy", that is,
it will match as many times as possible (given a particu-
lar starting location) while still allowing the rest of
the pattern to match. If you want it to match the minimum
number of times possible, follow the quantifier with a
"?".
There are other useful examples in that man page. The "U" modifier is something
that PHP added, it is not part of perl's regex syntax. It basically reverses the
greedy tendency, so that ALL of the quantifiers in a particular regex are ungreedy,
and the "?" makes them greedy. Without the "U", the normal (and perl
compatible)
behavior of "?" following a quantifier is to make it ungreedy.
HTH