PHP 5.3 的 ereg/eregi 替代品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9954064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ereg/eregi replacement for PHP 5.3
提问by Colin
I'm sorry to ask a question but I am useless when it comes to understanding regex code.
我很抱歉提出一个问题,但在理解正则表达式代码方面我毫无用处。
In a php module that I didn't write is the following function
在我没有写的php模块中是以下函数
function isURL($url = NULL) {
if($url==NULL) return false;
$protocol = '(http://|https://)';
$allowed = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';
$regex = "^". $protocol . // must include the protocol
'(' . $allowed . '{1,63}\.)+'. // 1 or several sub domains with a max of 63 chars
'[a-z]' . '{2,6}'; // followed by a TLD
if(eregi($regex, $url)==true) return true;
else return false;
}
Can some kind soul give me the replacement code for that with whatever is required to replace the eregi
有没有好心人可以给我替换代码,用什么来替换 eregi
回答by TMS
Good question - this is needed when you upgrade to PHP 5.3, where ereg
and eregi
functions are deprecated. To replace
好问题 - 当您升级到 PHP 5.3 时需要这样做,其中ereg
和eregi
函数已弃用。取代
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).
(i
第一个参数中的尾随表示 ignorecase 并对应于i
in eregi
- 只是在替换ereg
调用的情况下跳过)。
But be aware of differences between the new and old patterns! This pagelists 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.
但要注意新旧模式之间的差异!此页面列出了主要差异,但对于更复杂的正则表达式,您必须更详细地查看POSIX 正则表达式(由旧的 ereg/eregi/split 函数等支持)和PCRE之间的差异。
But in your example, you are just safe to replace the eregi call with:
但是在您的示例中,您可以安全地将 eregi 调用替换为:
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.)
(注意:这%
是一个分隔符;通常使用斜杠/
。您必须确保分隔符不在正则表达式中或将其转义。在您的示例中,斜杠是 $regex 的一部分,因此使用不同的字符更方便分隔符。)
回答by Roger Wolff
Palliative PHP 5.3 until you replace all deprecated functions
姑息性 PHP 5.3,直到您替换所有已弃用的函数
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); } }
回答by William Isted
Did you want a complete replacement to preg_match and eregi?
您想要完全替代 preg_match 和 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;
}
回答by Baba
eregi
is depreciated in PHP you have to use preg_match
eregi
在 PHP 中折旧,你必须使用 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.phpfor more information Thanks
请参阅http://php.net/manual/en/function.preg-match.php了解更多信息 谢谢
:)
:)