2012-02-04 24 views
1

那麼這是一個活動鏈接的功能,如果有人在文本消息框中添加鏈接。Php link active

我的問題是,它不顯示超過1個鏈接,如果有人把許多鏈接前:www.yahoo.com www.gmail.com www.facebook.com,那麼它只顯示第一個鏈接www.yahoo.com

function txt2link($text){ 
    // force http: on www. 
    $text = ereg_replace("www\.", "http://www.", $text); 
    // eliminate duplicates after force 
    $text = ereg_replace("http://http://www\.", "http://www.", $text); 
    $text = ereg_replace("https://http://www\.", "https://www.", $text); 

    // The Regular Expression filter 
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; 
    // Check if there is a url in the text 
    if(preg_match($reg_exUrl, $text, $url)) { 
     // make the urls hyper links 
     $text = preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text); 
    } // if no urls in the text just return the text 
    return ($text); 
} 

$url = "Alter pot waer it your pot http://css-tricks.com/snippets/php/find-urls-in- 
     text-make-links/ you may click the link www.yahoo.com or you may see what is the 
     http://www.youtube.com say, is it right?"; 

echo txt2link($url); 

您可以運行此代碼查看結果。

任何想法?

+1

我不明白。僅今天,我就看到十幾個人試圖從文本中提取網址時有相似(但不同的!)正則表達式。爲什麼會發生? PHP'get_urls'軟件包的開發者會在充滿他們錢的游泳池中游泳嗎? – Borealid 2012-02-04 06:52:18

+1

我想說的第一件事是溝渠ereg傾向於preg,它現在已經被廢棄了一段時間。 – quickshiftin 2012-02-04 06:55:27

+0

有沒有想過使用js來做這個客戶端網站? http://benalman.com/code/projects/javascript-linkify/docs/files/ba-linkify-js.html – Flukey 2012-02-04 10:15:20

回答

2

這是一個人在這裏幫助我建立了一段時間,我不知道它在棧上的位置了,但我仍然使用這個函數到目前爲止..這處理了一個鏡頭,有或沒有http:或沒有www。在大多數情況下,這是真的,我們可以進行一些改進,但總的來說,這項工作真的很好。

//for finding URLs within body of text and converting them to a clickable link 
//checks DNS to see if its valid and if the link HTML already exists ignores it. 
function titleHyper($text){ 
       $text = preg_replace("/(www\.)/is", "http://", $text); 
       $text = str_replace(array("http://http://","http://https://"), "http://", $text); 
       $text = str_replace(array("<a href='", "<a href=\"", "</a>", "'>", "\">"), "", $text); 
       $reg_exUrl = "/(http|https|ftp|ftps|)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; 
       preg_match_all($reg_exUrl, $text, $matches); 
       $usedPatterns = array(); 
       $context = stream_context_create(array(
       'http' => array(
       'timeout' => 5 
       ) 
       )); 
       foreach($matches[0] as $pattern){ 
        if(!array_key_exists($pattern, $usedPatterns)){ 
          $usedPatterns[$pattern]=true; 
          $the_contents = @file_get_contents($pattern, 0, $context); 
          if(substr(trim($pattern), 0, 8) != "https://"){ 
          $color = "#FF0000"; 
          } 
          if (empty($the_contents)) { 
          $title = $pattern; 
          } else { 
          preg_match("/<title>(.*)<\/title>/Umis", $the_contents, $title); 
          $title = $title[1]; 
          $color = "#00FF00";      
          //$title = htmlspecialchars($title, ENT_QUOTES); //saving data to database 
          }      
          $text = str_ireplace($pattern, "<a style='font-size: 14px; background-color: #FFFFFF; color: $color;' href='$pattern' rel='nofollow' TARGET='_blank'> $title </a>", $text); 

        } 
       } 
       return $text; 
} 
// titleHyper() in action example: 
//$text = "Some sample text with WWW.AOL.com<br />http://www.youtube.com/watch?v=YaxKiZfQcX8 <br />Anyone use www.myspace.com? <br />Some people are nuts, look at this stargate link at http://www.youtube.com/watch?v=ZKoUm6z5SzU&feature=grec_index , like aliens exist or something. http://www.youtube.com/watch?v=sfN-7HczmOU&feature=grec_index and here's a secure site https://familyhistory.hhs.gov, unless you use curl or allow secure connections it will never get a title. <br /> This is a not valid site http://zzzzzzz and this is a dead site http://zwzwzwxzw.com.<br /> Lastly lets try an already made hyperlink and see what it does <a href='http://tacobell.com'>taco bell</a>"; 
//echo titleHyper($text); 
+0

感謝@chris代碼。但是需要幾秒鐘才能加載,爲什麼? – user1161867 2012-02-04 07:03:31

+0

它檢查給定的URL的DNS的有效性。所以如果你鍵入AOL.com它檢查該鏈接上的DNS以查看它是否是活動鏈接,如果它不是它不會使它成爲鏈接。 – chris 2012-02-04 07:40:39

+0

它通過file_get_contents完成它的功能,它以一種原始格式打開網址,有點像查看源文件,如果其爲空或不返回任何內容,則忽略它,否則其「有效」 – chris 2012-02-04 07:42:46