php自動(dòng)給網(wǎng)址加上鏈接的方法
本文實(shí)例講述了php自動(dòng)給網(wǎng)址加上鏈接的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
這里自動(dòng)匹配頁(yè)面里的網(wǎng)址,包含http,ftp等,自動(dòng)給網(wǎng)址加上鏈接
function text2links($str='') { if($str=='' or !preg_match('/(http|www\.|@)/i', $str)) { return $str; } $lines = explode("\n", $str); $new_text = ''; while (list($k,$l) = each($lines)) { // replace links: $l = preg_replace("/([ \t]|^)www\./i", "\\1http://www.", $l); $l = preg_replace("/([ \t]|^)ftp\./i", "\\1ftp://ftp.", $l); $l = preg_replace("/(http:\/\/[^ )\r\n!]+)/i", "<a href=\"\\1\">\\1</a>", $l); $l = preg_replace("/(https:\/\/[^ )\r\n!]+)/i", "<a href=\"\\1\">\\1</a>", $l); $l = preg_replace("/(ftp:\/\/[^ )\r\n!]+)/i", "<a href=\"\\1\">\\1</a>", $l); $l = preg_replace( "/([-a-z0-9_]+(\.[_a-z0-9-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)+))/i", "<a href=\"mailto:\\1\">\\1</a>", $l); $new_text .= $l."\n"; } return $new_text; } //使用范例: $text = "Welcome www.dbjr.com.cn :-)"; print text2links($text);
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP+Swoole實(shí)現(xiàn)web版的shell客戶端詳解
這篇文章主要為大家詳細(xì)介紹了如何利用PHP+Swoole實(shí)現(xiàn)web版的shell客戶端,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-07-07解析smarty 截取字符串函數(shù) truncate的用法介紹
本篇文章是對(duì)smarty 截取字符串函數(shù) truncate的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php setcookie(name, value, expires, path, domain, secure) 參數(shù)
本篇文章是對(duì)php setcookie(name, value, expires, path, domain, secure) 參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP nl2br函數(shù) 將換行字符轉(zhuǎn)成 <br>
PHP nl2br函數(shù) 將換行字符轉(zhuǎn)成 <br>,不是很了解的朋友可以參考下。2009-08-08