2010-04-12 54 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