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.

You need to comment out the '!'

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?


Perl compatible regexs are faster* and more powerful. Course, writing a good Perl regex is an art form in itself (probably why O'Reilly released a book just on regexs), and takes some time (and headaches) to master (if one ever does master it).

The difference in the people who use one or the other? Probably nothing more than their background. Those of us who worked with Perl before php are more than likely gravitate towards preg. Those who didn't, or had little Perl regex experience more than likely went to the POSIX (ereg) style as it's a bit easier to pick up.

*I'm sure there are some out there who will dispute this.

--
John C. Nichel IV
Programmer/System Admin (ÜberGeek)
Dot Com Holdings of Buffalo
716.856.9675
jnichel@xxxxxxxxxxxxxxxxxxxxxxxxxxx

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