Re: The difference between ereg and preg?

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

 



Dave M G wrote:
PHP List,

Recently I wrote a piece of code to scrape data from an HTML page.

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.

That code looks like this:
ereg_replace("<!DOCTYPE(.*)<ul>", "", $htmlPage);

It works fine. But I noticed that on almost all the tutorial pages I looked at, they just about always used preg_replace, and not ereg_replace.

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

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

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?

Why does the ereg work for the command above, but preg not?

! in perl regular expressions means "not" so you need to escape it:

\!

pcre expressions are very close to the same as in perl, so you can easily move the regular expression from one language to the other (whether that's good or not is another thing). If you have any perl experience, they are easier to understand. If you don't, they are a little harder to understand to start off with but are more powerful once you work them out.

The ereg functions are simpler to use but miss a lot of functionality.

Also according to this page:
http://www.php.net/~derick/meeting-notes.html#move-ereg-to-pecl

ereg will be moved to pecl which means it will be less available (ie most hosts won't install / enable it).

--
Postgresql & php tutorials
http://www.designmagick.com/

--
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