【问题标题】:ereg/eregi replacement for PHP 5.3 [duplicate]ereg/eregi 替换 PHP 5.3 [重复]
【发布时间】:2012-04-14 19:02:14
【问题描述】:

很抱歉提出一个问题,但在理解正则表达式代码方面我毫无用处。

在我没有写的一个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;
}

哪位好心人能给我一个替换代码来替换eregi所需的任何东西

【问题讨论】:

  • 替换它的目的是什么?
  • @William,从 PHP 5.3 开始,eregeregisplit 等函数已被弃用(不仅被弃用,而且被完全删除)。 Read more.
  • @William,但是这个解决方案过于本地化了这个特殊情况......如果他将一些代码移植到 PHP 5.3,他需要更通用的解决方案。

标签: php eregi posix-ere


【解决方案1】:

姑息 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); } }

【讨论】:

  • 有趣的是,初始化构造 &$matches = [] 在我的特定 PHP 5.3 版本下似乎失败了...
【解决方案2】:

好问题 - 当您升级到 PHP 5.3 时需要这样做,其中不推荐使用 eregeregi 函数。替换

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

使用

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

(第一个参数中的尾随i 表示忽略大小写,对应于eregi 中的i - 如果替换ereg 调用,请跳过)。

但请注意新旧模式之间的差异! This page 列出了主要区别,但对于更复杂的正则表达式,您必须更详细地查看 POSIX regex(由旧的 ereg/eregi/split 函数等支持)和 PCRE 之间的区别。

但在您的示例中,您可以安全地将 eregi 调用替换为:

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

(注意:% 是分隔符;通常使用斜杠 /。您必须确保分隔符不在正则表达式中或对其进行转义。在您的示例中,斜杠是 $regex 的一部分,所以使用不同的字符作为分隔符更方便。)

【讨论】:

  • 非常感谢托马斯,你是明星。
  • sorry Tomas 你的解决方案给出了一个错误 preg_match() [function.preg-match]: Unknown modifier 'h' is that was that was that is that is that is that is that is that is that is that is that is that is that is that is that is that was that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that was that is that is that is that is that is that was that is that is that is that is that is that was that is that is that is that was that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that is that are that is that are that is that is that are that is that are that is that are that are that are that are that is that are that are that are that are that are that are that are that are that are that are that are that is that的是的是由 } 之后的 i 引起的
  • @Colin,好的,我明白了!这是因为分隔符 | 也在 $regex 中使用......所以我们必须使用不同的分隔符,这在 $regex 中不存在,例如%(希望我没有再次忽略某些东西 :-))。更新了我的答案。
  • 非常感谢您似乎成功了。最后一个问题。 eregi 在匹配字母字符时忽略大小写区别。我现在拥有的代码是否仍然这样做,或者我是否需要在 $allowed 中添加更多内容?
  • @Colin,不,ignorecase 选项是您在preg_match 的模式参数末尾看到的尾随“i”。这个新代码应该与旧代码完全等效。
【解决方案3】:

您想要完全替代 preg_match 和 eregi 吗?

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

或电子邮件:

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

【讨论】:

    【解决方案4】:

    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" ;
    }
    

    更多信息请查看http://php.net/manual/en/function.preg-match.php 谢谢

    :)

    【讨论】:

    • 您的替换正则表达式不适用相同的规则
    • 嘿,选民……你愿意给出我被否决的原因吗???
    • @meouw ...规则是验证有效的电子邮件地址...我错过了什么
    • 问题是关于用等价的东西替换 ereg 正则表达式 - 你的不等价 - 你的有一个可选的协议部分,例如,原来需要协议的地方
    • @meouw 有人只是给出了一个无正则表达式的例子,但没有被否决……我给的只是 url 验证的替代品。我认为你应该离开 Colin 来决定它是否相关对他或现在..我没有错误的代码......它可能不是 100% 但这并不意味着我应该被否决
    猜你喜欢
    • 1970-01-01
    • 2010-12-02
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    相关资源
    最近更新 更多