【问题标题】:Convert eregi to preg_match and ereg_replace to preg_replace [duplicate]将eregi转换为preg_match,将ereg_replace转换为preg_replace [重复]
【发布时间】:2013-02-10 07:57:41
【问题描述】:

我正在使用http://www.internoetics.com/2010/01/12/simple-whois-php-script/ PHP whois 脚本,我需要将其配置为最新的 PHP 版本,我需要将 ereg 和 eregi 转换为 preg_match 和 preg_replace

if ( (!eregi('^[a-zA-Z0-9-]+\.([a-zA-Z]{2,4})$', $domain)) && (!eregi('^[a-zA-Z0-9-]+\.([a-zA-Z]{2,4})+\.([a-zA-Z]{2,4})$', $domain)) ) $arrErrors['domi'] = 'Domain name appears to be invalid.';

function makeClickableLinks($text)
{
        $text = html_entity_decode($text);
        $text = " ".$text;
        $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                '<a href="\\1" target=_blank>\\1</a>', $text);
        $text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                '<a href="\\1" target=_blank>\\1</a>', $text);
        $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
        '\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
        $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
        '<a href="mailto:\\1" target=_blank>\\1</a>', $text);
        return $text;
}

谢谢

【问题讨论】:

    标签: php preg-replace eregi ereg


    【解决方案1】:

    对于 preg_* 函数,您只需在模式的开头和结尾添加分隔符,例如 /# 或其他。

    写这个

    if ( (!preg_match('/^[a-z\d-]+\.([a-z]{2,4})$/i', $domain)) && (!preg_match('/^[a-z\d-]+\.([a-z]{2,4})+\.([a-z]{2,4})$/', $domain)))
        echo $arrErrors['domi'] = 'Domain name appears to be invalid.';
    
    function makeClickableLinks($text)
    {
            $text = html_entity_decode($text);
            $text = " ".$text;
            $text = preg_replace('#(((f|ht){1}tp://)[-a-z\d@:%_\+.~\#?&//=]+)#',
                    '<a href="\\1" target=_blank>\\1</a>', $text);
            $text = preg_replace('#(((f|ht){1}tps://)[-a-z\d@:%_\+.~\#?&//=]+)#',
                    '<a href="\\1" target=_blank>\\1</a>', $text);
            $text = preg_replace('#([[:space:]()[{}])(www.[-a-z\d@:%_\+.~\#?&//=]+)#',
            '\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
            $text = preg_replace('#([_\.\da-z-]+@([\da-z][\da-z-]+\.)+[a-z]{2,3})#',
            '<a href="mailto:\\1" target=_blank>\\1</a>', $text);
            return $text;
    }
    

    【讨论】:

    • 你能告诉我怎么做吗? eb因为我不熟悉 reg 表达式.. :/
    • @Dance 我已经更新了我的答案
    • 谢谢,工作就像一个魅力!
    猜你喜欢
    • 2013-05-26
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多