Re: The difference between ereg and preg?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 04/08/06, Dave M G <martin@xxxxxxxxxxxxx> wrote:

It seemed that the main difference was that preg_replace required
forward slashes around the regular expression, like so:
preg_replace("/<!DOCTYPE(.*)<ul>/", "", $htmlPage);

It requires delimiters - slashes are conventional, but other
characters can be used.


But that didn't work, and returned an error.

What you've written here shouldn't return you an error - what did the
message say?

I suspect you were trying to match until </ul> rather than <ul> and
the pcre engine thought the forward slash was the end of your regular
expression.

Part of that code deleted all the unwanted text from the very top of the
page, where it says "<!DOCTYPE", all the way down to the first instance
of a "<ul>" tag.

It won't quite do that.

The (.*) matches as much as possible (it's called greedy matching) -
so it'll match and replace all the way down to the *last* instance of
a "<ul>" tag.

To make it match for the shortest length possible, put a question-mark
after the ".*" like so:

  preg_replace("/<!DOCTYPE(.*?)<ul>/", "", $htmlPage);

Since ereg was working, though, I figured I would just stick with it.

Still, I thought it worth asking:

Is there any reason why either ereg or preg would be more desirable over
the other?

pcre has a performance advantage, has more features and can use the
many regexps written for perl with few or no changes.

ereg is a posix standard.

 -robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux