2012-02-04 7 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

私はそれを取得しません。今日だけでも、私はテキストからURLを抽出しようとしている(しかし異なる!)正規表現を持つ十数人を見ました。なぜこれが起こるのですか? PHPの 'get_urls'パッケージの開発者は、お金でいっぱいのプールで泳ぐでしょうか? – Borealid

+1

私が言う最初のことは、プリグに賛成して逃げようとしていることです、それはしばらくの間非難されています。 – quickshiftin

+0

jsを使ってこのクライアントサイトをやってみたことがありますか? http://benalman.com/code/projects/javascript-linkify/docs/files/ba-linkify-js.html – Flukey

答えて

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

+0

それは与えられたURLのDNSの妥当性をチェックします。もしAOL.comとタイプすると、そのリンク上のDNSをチェックしてアクティブリンクかどうかを確認します。 – chris

+0

これはfile_get_contentsを使って行います。スピーチの意味での生の形式でURLを開き、ソースのように表示します。空であれば何も返しません。それ以外の場合は "有効" – chris