前面介紹過js把文本鏈接轉換成A標簽,現在看一個php的方法,這個是在 Silva 代碼的基礎上修改的,如果有不完善的地方大家可提出來.
需要提取的內容如下:
<a href="http://baidu.com">http://baidu.com</a>這是第一個A標簽,
<a href="http://blog.baidu.com">成長腳印-專注于互聯網發展</a>這是第二個A標簽。
http://www.49028c.com這是第一個需要被提取的URL地址,
http://blog.baidu.com這是第二個需要被提取的URL地址'。
<img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif">,這是一個IMG標簽
類似微博中的自動提取URL為超鏈接地址,即將紅色標記的內容提取出來添加A標簽,轉換成真正的超鏈接,網上搜索了很久,沒有找到一個切實可行的解決方案,大都只是簡單的提取URL(A標簽和IMG標簽內的地址也被提取替換了),并不能滿足以上需求,正則表達式中也沒發現能夠實現提取時過濾掉A標簽的方法,于是轉換了一下思路,即先將所有的A標簽和IMG標簽正則替換為某一個統一的標記,然后再提取URL地址替換為超鏈接,最后再將統一的標記還原替換為以前的A標簽和IMG標簽便解決了,代碼如下:
- function linkAdd($content){
- //提取替換出所有A標簽(統一標記<{link}>)
- preg_match_all('/<a.*?href=".*?".*?>.*?</a>/i',$content,$linkList);
- $linkList=$linkList[0];
- $str=preg_replace('/<a.*?href=".*?".*?>.*?</a>/i','<{link}>',$content);
- //提取替換出所有的IMG標簽(統一標記<{img}>)
- preg_match_all('/<img[^>]+>/im',$content,$imgList);
- $imgList=$imgList[0];
- $str=preg_replace('/<img[^>]+>/im','<{img}>',$str);
- //提取替換標準的URL地址
- $str=preg_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)','<a href="/0" target="_blank">/0</a>',$str);
- //還原A統一標記為原來的A標簽
- $arrLen=count($linkList);
- for($i=0;$i<$arrLen;$i++){
- $str=preg_replace('/<{link}>/',$linkList[$i],$str,1);
- }
- //還原IMG統一標記為原來的IMG標簽
- $arrLen2=count($imgList);
- for($i=0;$i<$arrLen2;$i++){
- $str=preg_replace('/<{img}>/',$imgList[$i],$str,1);
- }
- return $str;
- }
- $content='
- <a href="http://baidu.com">http://baidu.com</a>這是第一個A標簽,
- <a href="http://blog.baidu.com">成長腳印-專注于互聯網發展</a>這是第二個A標簽。
- http://www.49028c.com這是第一個需要被提取的URL地址,
- http://blog.baidu.com這是第二個需要被提取的URL地址。
- <img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif">,這是一個IMG標簽';
- echo linkAdd($content);
返回的內容為:
<a href="http://baidu.com">http://baidu.com</a>這是第一個A標簽, <a href="http://blog.baidu.com">成長腳印-專注于互聯網發展</a>這是第二個A標簽。 <a href="http://www.49028c.com" target="_blank">http://www.49028c.com</a>這是第一個需要被提取的URL地址, <a href="http://blog.baidu.com" target="_blank">http://blog.baidu.com</a>這是第二個需要被提取的URL地址。
<img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif">,這是一個IMG標簽
即為我們想要的內容.
例2,代碼如下:
- /**
- * PHP 版本 在 Silva 代碼的基礎上修改的
- * 將URL地址轉化為完整的A標簽鏈接代碼
- */
- /** =============================================
- NAME : replace_URLtolink()
- VERSION : 1.0
- AUTHOR : J de Silva
- DESCRIPTION : returns VOID; handles converting
- URLs into clickable links off a string.
- TYPE : functions
- ============================================= */
- function replace_URLtolink($text) {
- // grab anything that looks like a URL...
- $urls = array();
- // build the patterns
- $scheme = '(https?://|ftps?://)?';
- $www = '([w]+.)';
- $ip = '(d{1,3}.d{1,3}.d{1,3}.d{1,3})';
- $name = '([w0-9]+)';
- $tld = '(w{2,4})';
- $port = '(:[0-9]+)?';
- $the_rest = '(/?([w#!:.?+=&%@!-/]+))?';
- $pattern = $scheme.'('.$ip.$port.'|'.$www.$name.$tld.$port.')'.$the_rest;
- $pattern = '/'.$pattern.'/is';
- // Get the URLs
- $c = preg_match_all($pattern, $text, $m);
- if ($c) {
- $urls = $m[0];
- }
- // Replace all the URLs
- if (! emptyempty($urls)) {
- foreach ($urls as $url) {
- $pos = strpos('http://', $url);
- if (($pos && $pos != 0) || !$pos) {
- $fullurl = 'http://'.$url;
- } else {
- $fullurl = $url;
- }
- $link = ''.$url.'';
- $text = str_replace($url, $link, $text);
- }
- }
- return $text;
- }
例一測試過,例二沒有測試過大家測試一下看那個好用用那個吧.
新聞熱點
疑難解答