ereg/eregi替代PHP 5.3 [英] ereg/eregi replacement for PHP 5.3

查看:80
本文介绍了ereg/eregi替代PHP 5.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉提出一个问题,但是对于理解正则表达式代码毫无用处.

I'm sorry to ask a question but I am useless when it comes to understanding regex code.

在我未编写的php模块中,以下函数

In a php module that I didn't write is the following function

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

Can some kind soul give me the replacement code for that with whatever is required to replace the eregi

推荐答案

好问题-升级到不支持eregeregi函数的PHP 5.3时,这是必需的.替换

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) 

使用

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

(第一个参数中的尾随i表示忽略大小写,并且与eregi中的i相对应-在替换ereg调用的情况下只是跳过).

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

但是请注意新旧模式之间的差异! 此页面列出了主要区别,但是对于更复杂的正则表达式,您必须详细了解 POSIX regex (由旧的ereg/eregi/split支持)之间的区别功能等)和 PCRE .

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.

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

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

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

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

(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.)

这篇关于ereg/eregi替代PHP 5.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆