那麼這是一個活動鏈接的功能,如果有人在文本消息框中添加鏈接。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);
您可以運行此代碼查看結果。
任何想法?
我不明白。僅今天,我就看到十幾個人試圖從文本中提取網址時有相似(但不同的!)正則表達式。爲什麼會發生? PHP'get_urls'軟件包的開發者會在充滿他們錢的游泳池中游泳嗎? – Borealid 2012-02-04 06:52:18
我想說的第一件事是溝渠ereg傾向於preg,它現在已經被廢棄了一段時間。 – quickshiftin 2012-02-04 06:55:27
有沒有想過使用js來做這個客戶端網站? http://benalman.com/code/projects/javascript-linkify/docs/files/ba-linkify-js.html – Flukey 2012-02-04 10:15:20