preg_match works, eregi doesn't

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
calebsg
Forum Commoner
Posts: 28
Joined: Tue Jun 18, 2002 10:41 am

preg_match works, eregi doesn't

Post by calebsg »

The following code works great:

Code: Select all

if (preg_match ("/php/i", "PHP is the web scripting language of choice.")) {
    print "A match was found.";
} else {
    print "A match was not found.";
}
and the following doesn't work at all:

Code: Select all

if (eregi ("/php/i", "PHP is the web scripting language of choice.")) {
    print "A match was found.";
} else {
    print "A match was not found.";
}
Why would that be?
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

the second one should be....

Code: Select all

if (eregi ("php", "PHP is the web scripting language of choice.")) {
  print "A match was found.";
} else {
  print "A match was not found.";
}

note that the forward slashes and the 'i' have been removed
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Yes, that is because preg and ereg use two different types of regex's, preg functions are PCRE, or Perl Compatible Regular Expressions, where as teh ereg are not.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

do the ereg-functions have ANY advantage?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Nope, not really, unless you are used to those types of RegEx's. EREG is POSIX compliant.

PCRE is faster though, and I just stick to that.
Post Reply