ereg/eregi replacement for PHP 5.3 [duplicate]

走远了吗. 提交于 2019-11-27 20:39:54

Good question - this is needed when you upgrade to PHP 5.3, where ereg and eregi functions are deprecated. To replace

eregi('pattern', $string, $matches) 

use

preg_match('/pattern/i', $string, $matches)

(the trailing i in the first argument means ignorecase and corresponds to the i in eregi - just skip in case of replacing ereg call).

But be aware of differences between the new and old patterns! This page lists the main differences, but for more complicated regular expressions you have to look in more detail at the differences between POSIX regex (supported by the old ereg/eregi/split functions etc.) and the PCRE.

But in your example, you are just safe to replace the eregi call with:

if (preg_match("%{$regex}%i", $url))
    return true;

(note: the % is a delimiter; normally slash / is used. You have either to ensure that the delimiter is not in the regex or escape it. In your example slashes are part of the $regex so it is more convenient to use different character as delimiter.)

Roger Wolff

Palliative PHP 5.3 until you replace all deprecated functions

if(!function_exists('ereg'))            { function ereg($pattern, $subject, &$matches = []) { return preg_match('/'.$pattern.'/', $subject, $matches); } }
if(!function_exists('eregi'))           { function eregi($pattern, $subject, &$matches = []) { return preg_match('/'.$pattern.'/i', $subject, $matches); } }
if(!function_exists('ereg_replace'))    { function ereg_replace($pattern, $replacement, $string) { return preg_replace('/'.$pattern.'/', $replacement, $string); } }
if(!function_exists('eregi_replace'))   { function eregi_replace($pattern, $replacement, $string) { return preg_replace('/'.$pattern.'/i', $replacement, $string); } }
if(!function_exists('split'))           { function split($pattern, $subject, $limit = -1) { return preg_split('/'.$pattern.'/', $subject, $limit); } }
if(!function_exists('spliti'))          { function spliti($pattern, $subject, $limit = -1) { return preg_split('/'.$pattern.'/i', $subject, $limit); } }

Did you want a complete replacement to preg_match and eregi?

if(!filter_var($URI, FILTER_VALIDATE_URL))
{ 
return false;
} else {
return true;
}

Or for Email:

if(!filter_var($EMAIL, FILTER_VALIDATE_EMAIL))
{ 
return false;
} else {
return true;
}

eregi is depreciated in PHP you have to use preg_match

function isValidURL($url)
{
    return preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $url);
}


if(isValidURL("http://google.com"))
{
    echo "Good URL" ;
}
else
{
    echo "Bad Url" ;
}

Please see http://php.net/manual/en/function.preg-match.php for more information Thanks

:)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!