0

您好,我在 2 或 3 年前写过一些脚本。现在它没有运行。我的脚本:

<?php
 function tolink($text){

    $text = " ".$text;
    $text = ereg_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '<a href="\\1" target="_blank" rel="nofollow">\\1</a>', $text);
    $text = ereg_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '<a href="\\1" target="_blank" rel="nofollow">\\1</a>', $text);
    $text = ereg_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
    '\\1<a href="http://\\2" target="_blank" rel="nofollow">\\2</a>', $text);
    $text = ereg_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 替换 ereg_replace 时,它​​给了我一个错误。

我需要你的帮助...谢谢...

4

1 回答 1

0

PHP 中的所有 PCRE 函数都要求您用一对分隔符将正/则表达式括起来,例如在本例中将正则表达式括起来:

preg_replace ('/regexp_to_match/', 'replacement', $text);

这允许您在第二个分隔符之后将修饰符附加到正则表达式,如下所示:

preg_replace ('#regexp_to_match#i', 'replacement', $text);

它执行不区分大小写的匹配。

从这两个示例中可以看出,分隔符可以是任何字符。正则表达式的第一个字符作为分隔符,然后在末尾查找匹配项。如果字符是括号字符,它会寻找它的相反字符,例如

preg_match ('<x*>', $text);

preg_replace()是您第一次替换的版本。

  $text = preg_replace('<(((f|ht){1}tp://)[-\w@:%+.~#?&/=]+)>',
        '<a href="$1" target="_blank" rel="nofollow">$1</a>', $text);

我还简化了正则表达式:我使用单词字符而\w不是明确列出字母数字,\删除了. 此外,这些天在替换中是首选,而不是.+[...]///$1\\1

于 2013-08-24T23:12:02.503 回答