Php链接活跃

时间:2012-02-04 06:48:51

标签: php regex

如果有人在带有文本的消息框中放置链接,这就是激活链接的功能。

我的问题是它没有显示超过1个链接,如果有人放了许多链接ex:www.yahoo.com www.gmail.com www.facebook.com,那么它只显示第一个链接www.yahoo.com < / p>

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 个答案:

答案 0 :(得分:2)

这是有人在这里帮助我建立了一段时间,我不知道它在堆栈上的位置了但我仍然使用此功能来约会..这可以一次性处理许多网址,有或没有http:with或没有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);