Fix `ereg is deprecated` errors in PHP 5.3
If you upgraded to PHP 5.3, chances are high you’re going to run into a few warnings or deprecated function messages.
An example is the ereg family of functions, which are gone for good, as they were slower and felt less familiar than the alternative Perl-compatible preg family.
To migrate ereg():
ereg('\.([^\.]*$)', $this->file_src_name, $extension);
becomes
preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);
Notice that I wrapped the pattern (\.([^\.]*$)) around / /, which are RegExp delimiters. If you find yourself escaping / too much (for an URL for example), you might want to use the # delimiter instead.
To migrate ereg_replace():
$this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body);
becomes
$this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body);
Again, I just added delimiters to the pattern.
If you are using eregi functions (which are the case-insensitive version of ereg), you’ll notice there’re no equivalent pregi functions. This is because this functionality is handled by RegExp modifiers.
Basically, to make the pattern match characters in a case-insensitive way, append i after the delimiter:
eregi('\.([^\.]*$)', $this->file_src_name, $extension);
becomes
preg_match('/\.([^\.]*$)/i', $this->file_src_name, $extension);
-
very complex stuff but thank you for trying to make sense out of this error
-
If someone is having the same problem the solution weren’t single quotes but DOUBLE “”
-
Damn it doesn’t work I didn’t notice it because the error wasn’t displayed anymore but it returned wrong result
-
Hello I have this function but somehow I cannot get it working with preg_match can you please point me to sollution or write it here.
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
Thanks-
Solved i had to insert single quotes:
if (preg_match(‘/$Match/i’,$_SERVER['HTTP_USER_AGENT']))
-
-
great thanks for this tricks
-
coll. nice dara man
-
Thankss alot that help fully
-
Hola, tengo este código y no se qué cambiar para que funcione:
if (eregi(‘^([a-z]{2,3})(-[a-z]{2,3})?(;q=[0-9.]+)?$’, trim($s), $r))Muchas gracias.
-
hi, thanks.usefull info
-
Thanks, very useful information.! i have some issue with deprecated syntax in php and need to replace it one by one. fiuh..
-
Thank you for neat explanation how to fix the problem.
-
sasasa
-
-
Warning: strtoupper() expects parameter 1 to be string, array given in D:\xampp\htdocs\……\libraries\joomla\environment\request.php on line 97
can anyone help how to remove this warning….
-
couldnt match with your solution
any help?if (!eregi(“(^\.\.?)”, $file)) {
-
Hi… that was really a helpfull post.. Thnx…
-
Nice post, it was useful to fix ereg deprecated warnings in oscommerce, thank you
-
Not working your eregi(). It is your false statement. Don’t write this in future. OKAY ?
-
Deprecated: Function eregi() is deprecated in C:\wamp\www\memories-on-gold\includes\classes\language.php on line 87 any one help me.
-
Nice topic . Thank you for posting it !
-
Tui ki bojis nice topic ar ?
-
-
Thanx alot its very helpful
-
Thanks for this page. It is really clear.
-
We had a similar issue, but our ereg was comparing the contents of a variable to something else – ereg($AllowedTypes,getExt($sFileName). I found a workaround that may help alot of people here…
What preg_match (the replacement for ereg) is looking for is delimiters around the value. THis works great, unless you have a variable that’s used elsewhere and changing the value won’t work.
What I did was to write the line as such:
preg_match(“#”.$AllowedTypes.”#”,getExt($sFileName)
effectively enclosing the value in #s as the delimiter. BTW – the ‘/’ is not the required delmititer, it can be just about anything, as long as it’s the same on both ends. Adding the # front and back did the trick. A hack? Yeah, probably, but it does work. Saved my ass, for sure.
-
I’m having troubles using preg_match() on a constant.
How to convert this old line? BASEURL is a constant.eregi(BASEURL, $_SERVER['HTTP_REFERER'])
-
Installed osC 2.2 on web server and went fine. To develop locally, I installed XAMPP on local computer, then installed osC 2.2 locally and got this error. Great post, easy to follow, fixed my problem!
-
I have this problem
Deprecated: Function ereg() is deprecated in C:\xampp\htdocs\member.php on line 109
if (!ereg(“MSIE”,$_SERVER['HTTP_USER_AGENT']))
How can i fix this?
-
I have this problem:
Function eregi() is deprecated in C:\xampp\htdocs\catalog\admin\file_manager.php on line 29
if (!eregi(‘^’ . DIR_FS_DOCUMENT_ROOT, $current_path)) $current_path = DIR_FS_DOCUMENT_ROOT;
How can i fix this?
Thanks
-
Try this
if (!preg_match(‘#^’ . DIR_FS_DOCUMENT_ROOT.’#i’, $current_path)) $current_path = DIR_FS_DOCUMENT_ROOT;
-
-
Set the error reporting to E_ALL ^E_DEPRECATED where it is currently being set to E_ALL
change error_reporting(E_ALL); to
error_reporting(E_ALL ^E_DEPRECATED);and everything will be perfect.
-
hi
i have appserv 2.6.0 and i setup oscommerce 2.2rc and i recive this errorFatal error: Call to undefined function get_magic_quotes_gpc() in C:\AppServ\www\catalog\admin\includes\functions\compatibility.php on line 46
-
Hi!
I use sql_regcase, that is is deprecated.
Did you know an sql_regcase alternative ?
Example (to remove sql syntax):
$string = preg_replace(sql_regcase(“/(from|select|insert|delete|where|drop table|show tables|#|\*|–|\\\\)/”),”",$string);Thank you!
-
Sorry for the newbie question…
This is the answer:
$string = preg_replace(“/(from|select|insert|delete|where|drop table|show tables|#|\*|–|\\\\)/i”,”",$string);
This is because ’sql_regcase’ just make the regular expression for case insensitive match, so… just put ‘i’ in the last ‘/’.
Simple… but, why I don’t saw this before…
Thank you very much!
-
-
Hvala na postu!
Thank You for this post!
Very elegant solution! -
Thanks for this post it’s just exactly what i was looking for
-
Thaaaaaaaaaaanks its working !
-
Thanks for this, fixed my problem after we upgraded PHP to 5.3 and our Wordpress theme choked. Much appreciated!
-
Thanks, ymmd
-
so, what would i edit to make this work properly:
elseif(!eregi(“http://”,$uri) && !eregi(“https://”,$uri))
anybody? im really stuck! thanks in advance
-
A very usefull post, it saved my day. Thnx
-
Thanks for this! It helped me writing a Perl script that find all these deprecated functions.
You can download the script here:
http://nthinking.net/index.html#reDeprec.html -
Great site. helps me a lot.
-
Hi.. ereg() with preg_match() is working well
-
what about this
if (isset($_SERVER['HTTP_HOST']) AND isset($_SERVER['HTTP_REFERER'])) {
$visitorInfo['referer'] = (!eregi($_SERVER['HTTP_HOST'],$_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : “” ;
} else {
$visitorInfo['referer'] = “”;
} -
Hi, thanks for your writing. I fixed almost all deprecations as your guide, but except this code:
if (preg_match(‘^’.$arrParam[0].’=', $value))
When I put / / as delimiter before and after ^ to become:
if (preg_match(‘/^/’.$arrParam[0].’=', $value))
I keep getting error as:
Unknown modifier ‘r’ in C:/Program Files/EasyPHP5.3.0/www/taskfreak/include/classes/tzn_generic.php
Note: tzn_generic.php is the file I put above code.
Please help me.
Many thanks,
Seri -
Hi.
Can you please tell me what should I change in the code below to make things work:
$check_url = str_replace(“\”", “”, $check_url);
if ((eregi(“]*script*\”?[^>]*>”, $check_url)) || (eregi(“]*object*\”?[^>]*>”, $check_url)) ||
(eregi(“]*iframe*\”?[^>]*>”, $check_url)) || (eregi(“]*applet*\”?[^>]*>”, $check_url)) ||
(eregi(“]*meta*\”?[^>]*>”, $check_url)) || (eregi(“]*style*\”?[^>]*>”, $check_url)) ||
(eregi(“]*form*\”?[^>]*>”, $check_url)) || (eregi(“\([^>]*\”?[^)]*\)”, $check_url)) ||
(eregi(“\”", $check_url))) {I got this error:
Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 35Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 35
Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 36
Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 36
Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 37
Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 37
Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 38
Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 38
Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 39
Thank you.
-
Can I ask a really stupid question……what file am i supposed to edit? i cant find that code in the php.ini file. Am I being stupid??!!
-
doesen’t browser informs you about error when you test your project? it should say where error is. file and line no.
-
-
can’t resolve this one
$msg =ereg_replace(“(http|https|ftp|gopher|news)://([$ACCEPT_CHARS]+)”, “\\1://\\2” , $msg);
-
Thank you!
-
thx!
-
Thanks alot its working. also i have a problem with split
Function split() is deprecated if you have any idea can u share with us
-
A lot of thanks!!
-
It’s worth noting that whilst ereg returns FALSE when there is no match, preg_match returns 0.
-
Thank you very much for this clear and simple explanation ! Much appreciated.
sasasa