Jump to content

Archived

This topic is now archived and is closed to further replies.

.josh

ereg and "deprecated" error

Recommended Posts

We get posts asking about this error on a fairly regular basis, so here's a sticky detailing the error and what to do to fix it.

 

PHP has a number of POSIX regex functions for performing regex matching/replacing/splitting.  All of these functions are deprecated, which means they are currently still available in php, but are going to eventually be removed from a future version of PHP. 

 

This is an "annoyance" to you now, because it causes an error, which may or may not show up, depending on what your error reporting settings are.  This is bad news for you in the future, because your code will break when you upgrade to a future version of PHP that no longer supports these functions. 

 

The solution is to convert the POSIX functions to one of the PCRE regex functions instead.

 

Here is the manual entry summarizing the differences, as well as what the PCRE function replacements are.

 

For most cases, you simply have to pick the PCRE equivalent and wrap a delimiter around your pattern.  You may also need to use the i modifier if you are using a case-insensitive POSIX function (eg: eregi vs. ereg).

 

Example: Check if a username is only numbers, letters (case-insensitive) and 6-12 chars long.

 

POSIX regex

if ( eregi("[a-z0-9]{6,12}",$username) ) {
  // good username!
} else {
  // bad username!
}

 

PCRE regex

if ( preg_match("~[a-z0-9]{6,12}~i",$username) ) {
  // good username!
} else {
  // bad username!
}

 

In the PCRE regex example, I use ~ as the delimiter and added the i modifier to make the regex case-insenstive.

 

If doing this doesn't fix your problem, then more than likely your pattern itself has POSIX-specific stuff in it, and you will need to change that, in which case, feel free to post in this forum asking for help.

Share this post


Link to post
Share on other sites

Much needed. The manual entry is very well done, and this is just icing on the cake!

 

Now we just need a sticky for the 'expects a resource - boolean given'

Share this post


Link to post
Share on other sites

Much needed. The manual entry is very well done, and this is just icing on the cake!

 

Now we just need a sticky for the 'expects a resource - boolean given'

Agreed, on both points. I can volunteer to write that one, if you guys will proof it for me :)

Share this post


Link to post
Share on other sites

Much needed. The manual entry is very well done, and this is just icing on the cake!

 

Now we just need a sticky for the 'expects a resource - boolean given'

Agreed, on both points. I can volunteer to write that one, if you guys will proof it for me :)

 

Talk is cheap! ;)

Share this post


Link to post
Share on other sites

I've actually been working on some tutorials for my own blog which is not yet launched, I was going to do one on debugging the mysql/i errors like that. I'll start working on it now :-P Give me something to do this weekend in my 20 minutes of free time when the baby is asleep ;)

Share this post


Link to post
Share on other sites

@.josh

...

All of these functions are deprecated, which means they are currently still available in php, but are going to eventually be removed from a future version of PHP. 

...

 

Does this include the multi-byte versions; i.e. mb_ereg_match?

 

I ask this because I have never seen the mb_ereg* functions mentioned in relation to any discussion on regular expressions. I just think it would be good to make the sticky "complete" by mentioning these one way or the other.

 

I came across them (in the manual) recently. while I was reworking my site into utf-8.

Share this post


Link to post
Share on other sites

@.josh

...

All of these functions are deprecated, which means they are currently still available in php, but are going to eventually be removed from a future version of PHP. 

...

 

Does this include the multi-byte versions; i.e. mb_ereg_match?

 

I does not appear to be so. The first source would be the manual, and since there is no big pink "Depreciated" message it has not been thought of being depreciated.

Share this post


Link to post
Share on other sites

mbstring is a different extension than ereg.

 

Why mbstring uses POSIX rather than PCRE, I don't know

Share this post


Link to post
Share on other sites
Was that an oversight, or did they specifically decide to make an exception?

 

The mbstring regex functions are only similar by name.  They use the Oniguruma regex engine to get the work done, not POSIX nor PCRE.  Only the POSIX family of functions are deprecated at this time.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.