2010-04-12 56 views
-2

我需要帮助转换eregi_replace到的preg_replace(因为在PHP5它贬值)到了preg_replace:转换Eregi_replace在PHP

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

(原来的文字链接和电子邮件为超链接,以便用户可以点击他们)

回答

6

首先查看手册中POSIX和PCRE表达式之间的list of differences

如果您的表情并不复杂,通常意味着您可以简单地将分隔符放在$pattern参数的附近,并切换到使用preg系列函数。在你的情况,你可以这样做:

function makeClickableLinks($text) 
{ 
$text = preg_replace('/(((f|ht){1}tp:\/\/)[[email protected]:%_\+.~#?&\/\/=]+)/i', 
         '<a href="\\1">\\1</a>', $text); 
$text = preg_replace('/([[:space:]()[{}])(www.[[email protected]:%_\+.~#?&\/\/=]+)/i', 
         '\\1<a href="http://\\2">\\2</a>', $text); 
$text = preg_replace('/([_\.0-9a-z-][email protected]([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/i', 
         '<a href="mailto:\\1">\\1</a>', $text); 
return $text; 
} 

注意周围的图案/字符,分隔符后i标志。我很快测试了它,并且它对基本URL起作用。你可能想要更彻底地测试它。

+0

谢谢你的回答,我会查看你已发布的链接,并将使用你的建议将其他eregi_replace转换为preg_replace。 – alexy13 2010-04-12 23:55:30

+0

梦幻般的答案。既用于转换函数(这是常用的),也用于指向该链接的指针。我正在浏览php手册,但没有看到该页面。 – Gerry 2010-04-20 03:36:11