Jump to content

ereg and "deprecated" error


.josh

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.

Link to comment
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 :)

Link to comment
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! ;)

Link to comment
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 ;)

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.