我需要帮助将PHP eregi_replace转换为preg

时间:2012-02-19 00:35:28

标签: php replace deprecated eregi

我知道它有点长。我想赶上最后期限,如果有人能帮助我,我很乐意欣赏它。我的另一个朋友程序员制作了这个PHP脚本,但不幸的是它已经被弃用了,但是不起作用。任何人都可以将其转换为preg吗?我对eregi,preg,rejex等一无所知。

    function tolink($text){

    $text = " ".$text;

    $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',

    '<a href="\\1" target="_blank" rel="nofollow">\\1</a>', $text);

    $text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',

    '<a href="\\1" target="_blank" rel="nofollow">\\1</a>', $text);

    $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',

    '\\1<a href="http://\\2" target="_blank" rel="nofollow">\\2</a>', $text);

    $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4})',

    '<a href="mailto:\\1"  rel="nofollow">\\1</a>', $text);

    return $text;

    }

1 个答案:

答案 0 :(得分:0)

不会是这样的

  function tolink($text){
 $text = " ".$text;
 $text = preg_replace("/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/","<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$1</a>", $text);
$text = preg_replace("/(((f|ht){1}tps:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/","<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$1</a>", $text);
 $text = preg_replace("/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/",
                      "$1<a href=\"http://$2\" target=\"_blank\" rel=\"nofollow\">$2</a>", $text);

 $text = preg_replace('/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4})/',
                      "<a href=\"mailto:$1\"  rel=\"nofollow\">$1</a>", $text);
 return $text;

}

你也可以在preg_replace模式的末尾添加i,以便匹配小写和大写字符

这就是我的意思

 preg_replace('/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i' .....