RE: [PHP] The difference between ereg and preg?
| From: | Ford, Mike | Date: | Fri, 04 Aug 2006 10:25:06 +0000 |
| Subject: | RE: [PHP] The difference between ereg and preg? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-240098@lists.php.net to get a copy of this message | ||
On 04 August 2006 10:52, Dave M G wrote:
> Chris, Ligaya, Dave,
>
> Thank you for responding. I understand the difference in principle
> between ereg and preg much better now.
>
> Chris wrote:
> > ! in perl regular expressions means "not" so you need to escape it:
> > \!
AFAIR, that's only true in the (?! assertion sequence.
> Still, when including that escape character, the following preg
> expression does not find any matching text:
> preg_replace("/<\!DOCTYPE(.*)<ul>/", "", $htmlPage);
>
> Whereas this ereg expression does find a match:
> ereg_replace("<!DOCTYPE(.*)<ul>", "", $htmlPage);
>
> What do I need to do to make the preg expression succeed just as the
> ereg expression does?
By default, . in preg_* patterns does not match newlines. If you are matching in a multiline
string, use the s modifier to change this:
preg_replace("/<!DOCTYPE(.*)<ul>/s", "", $htmlPage);
You also don't need the parentheses -- these will capture the entire matched expression for use
in backreferences, but as you don't have any it's then immediately thrown away. So just
use:
preg_replace("/<!DOCTYPE.*<ul>/s", "", $htmlPage);
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm