2013-07-25 39 views
1
function makeLinksInTheContent($html) 
{ 
    $html= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" rel=\"nofollow\" >$3</a>", $html); 
    $html= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" rel=\"nofollow\" >$3</a>", $html); 
    $html= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:[email protected]$3\" rel=\"nofollow\">[email protected]$3</a>", $html); 

    return($html); 
} 

這是我的代碼。preg_replace用於替換匹配網址的字符串

我的需求是自動鏈接的URL。使用preg_replace查找網址並將鏈接設置爲此網址。

例如:「一個頁面包含www.google.com。」如果我將此內容傳遞給makeLinksInTheContent($ html),它將返回「一頁包含www.google.com」。

但是下面的url格式沒有被鏈接。

  1. http://www.google.com

  2. www.test.com()和[] & ^%$#@ + | @#$%^ &()_ +} {:!「? > <,。/;'[] = - 09〜`.co,in,com.com

  3. http://www.test.com()and [] & ^%$#@!+ | !@#$%^ &()_ +} {:「?> <,。/;'[] = - 09〜`.co,in,com.com

  4. https://www.test.com()和[] & ^%$#@!+ |!@#$%^ &()_ +} {:「?> <,。/;'[] = -09〜`.co,in,com.com

  5. ftp://www.test.com()and [] & ^%$#@!+ |!@#$%^ &()_ +} {:?「> <,/;'[] = - 09〜`.CO,在,com.com

我想我的正則表達式有一些錯誤。請給我們建議。

+0

我已經回答了這個問題之前...?這個EXACT問題 – SmokeyPHP

+0

[preg \ _replace替換匹配url的字符串]的副本(http://stackoverflow.com/questions/17783918/preg-replace-to-replace-string-for-matching-url) – SmokeyPHP

回答

0

我的回答你最後一次問這個:

preg_replace to replace string for matching url

我可以建議用我的功能 - 只要你的數據測試,對我的作品

function parse_links($string,$mailto=true) 
{ 
    $preg_r = "#(((https?|ftp)(://)?)?[a-zA-Z0-9-_.]{1,64}\.[a-zA-Z0-9-_.]{1,128}\.[a-z]{2,4}(\.[a-z]{2,4})?(/([^ ]{1,256}))?/?)#"; 
    $string = preg_replace($preg_r,"<a href=\"http://$1\">$1</a>",explode(" ",$string)); 
    if($mailto) 
    { 
     $preg_r2 = "/([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z_\-\.][-\w]*[0-9a-zA-Z_\-\.]\.)+[a-zA-Z]{2,9})/"; 
     $string = preg_replace($preg_r2,"<a href=\"mailto:$1\">$1</a>",$string); 
    } 
    return implode(" ",$string); 
} 

只是通過假的第二個參數如果您不想創建電子郵件鏈接

+0

以下網址沒有鏈接。 (www.google.com),www.test。!?。com()和[]&^%$#@ + | @#$%^&()_ +} {:「><,/;'[] = - 09〜'.CO,在, com.com,https://www.test.com()和[]&^%$#@!+ |!@#$%^&()_ +} {:「?><,。/;' [] = - 09〜'.co,in,com.com – user2349080

+0

當我測試字符串'www.google.com''www.test.com'和'https://www.test.com ' - 你還希望捕捉什麼? – SmokeyPHP

+0

http://phpfiddle.org/lite/code/zuv-y3k(點擊右上角的運行) – SmokeyPHP

3

在這種情況下,您可以使用preg_replace_callbackRead more

功能

<?php 
    function replace_urls($text = null) { 
    $regex = '/((http|ftp|https):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\[email protected]?^=%&amp;\/~+#-])?/'; 
    return preg_replace_callback($regex, function($m) { 
     $link = $name = $m[0]; 
     if (empty($m[1])) { 
     $link = "http://".$link; 
     } 
     return '<a href="'.$link.'" target="_blank" rel="nofollow">'.$name.'</a>'; 
    }, $text); 
    } 
?> 

使用

<?php 
    $text = "http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054 
    www.google.com 
    https://twitter.com/ 
    http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar"; 

    echo replace_urls($text); 
?> 

輸出

<a href="http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054" target="_blank" rel="nofollow">http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054</a> 
<a href="http://www.google.com" target="_blank" rel="nofollow">www.google.com</a> 
<a href="https://twitter.com/" target="_blank" rel="nofollow">https://twitter.com/</a> 
<a href="http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar" target="_blank" rel="nofollow">http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar</a> 
+0

以下網址沒有鏈接。 – user2349080