国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php將URL地址轉(zhuǎn)化為完整的A標(biāo)簽鏈接代碼

2024-05-04 21:47:55
字體:
供稿:網(wǎng)友

前面介紹過js把文本鏈接轉(zhuǎn)換成A標(biāo)簽,現(xiàn)在看一個php的方法,這個是在 Silva 代碼的基礎(chǔ)上修改的,如果有不完善的地方大家可提出來.

需要提取的內(nèi)容如下:

<a href="http://baidu.com">http://baidu.com</a>這是第一個A標(biāo)簽,

<a href="http://blog.baidu.com">成長腳印-專注于互聯(lián)網(wǎng)發(fā)展</a>這是第二個A標(biāo)簽。

http://m.survivalescaperooms.com這是第一個需要被提取的URL地址,

http://blog.baidu.com這是第二個需要被提取的URL地址'。

<img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif">,這是一個IMG標(biāo)簽

類似微博中的自動提取URL為超鏈接地址,即將紅色標(biāo)記的內(nèi)容提取出來添加A標(biāo)簽,轉(zhuǎn)換成真正的超鏈接,網(wǎng)上搜索了很久,沒有找到一個切實可行的解決方案,大都只是簡單的提取URL(A標(biāo)簽和IMG標(biāo)簽內(nèi)的地址也被提取替換了),并不能滿足以上需求,正則表達(dá)式中也沒發(fā)現(xiàn)能夠?qū)崿F(xiàn)提取時過濾掉A標(biāo)簽的方法,于是轉(zhuǎn)換了一下思路,即先將所有的A標(biāo)簽和IMG標(biāo)簽正則替換為某一個統(tǒng)一的標(biāo)記,然后再提取URL地址替換為超鏈接,最后再將統(tǒng)一的標(biāo)記還原替換為以前的A標(biāo)簽和IMG標(biāo)簽便解決了,代碼如下: 

  1. function linkAdd($content){ 
  2.  //提取替換出所有A標(biāo)簽(統(tǒng)一標(biāo)記<{link}>) 
  3.  preg_match_all('/<a.*?href=".*?".*?>.*?</a>/i',$content,$linkList); 
  4.  $linkList=$linkList[0]; 
  5.  $str=preg_replace('/<a.*?href=".*?".*?>.*?</a>/i','<{link}>',$content); 
  6.  
  7.  //提取替換出所有的IMG標(biāo)簽(統(tǒng)一標(biāo)記<{img}>) 
  8.  preg_match_all('/<img[^>]+>/im',$content,$imgList); 
  9.  $imgList=$imgList[0]; 
  10.  $str=preg_replace('/<img[^>]+>/im','<{img}>',$str); 
  11.  
  12.  //提取替換標(biāo)準(zhǔn)的URL地址 
  13.  $str=preg_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)','<a href="/0" target="_blank">/0</a>',$str); 
  14.  
  15.  //還原A統(tǒng)一標(biāo)記為原來的A標(biāo)簽 
  16.  $arrLen=count($linkList); 
  17.  for($i=0;$i<$arrLen;$i++){ 
  18.   $str=preg_replace('/<{link}>/',$linkList[$i],$str,1);  
  19.  } 
  20.  
  21.  //還原IMG統(tǒng)一標(biāo)記為原來的IMG標(biāo)簽 
  22.  $arrLen2=count($imgList); 
  23.  for($i=0;$i<$arrLen2;$i++){ 
  24.   $str=preg_replace('/<{img}>/',$imgList[$i],$str,1);  
  25.  } 
  26.  
  27.  return $str
  28.  
  29. $content=' 
  30. <a href="http://baidu.com">http://baidu.com</a>這是第一個A標(biāo)簽, 
  31. <a href="http://blog.baidu.com">成長腳印-專注于互聯(lián)網(wǎng)發(fā)展</a>這是第二個A標(biāo)簽。 
  32. http://m.survivalescaperooms.com這是第一個需要被提取的URL地址, 
  33. http://blog.baidu.com這是第二個需要被提取的URL地址。 
  34. <img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif">,這是一個IMG標(biāo)簽'; 
  35. echo linkAdd($content); 

返回的內(nèi)容為:

<a href="http://baidu.com">http://baidu.com</a>這是第一個A標(biāo)簽, <a href="http://blog.baidu.com">成長腳印-專注于互聯(lián)網(wǎng)發(fā)展</a>這是第二個A標(biāo)簽。 <a href="http://m.survivalescaperooms.com" target="_blank">http://m.survivalescaperooms.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標(biāo)簽

即為我們想要的內(nèi)容.

例2,代碼如下:

  1. /** 
  2.  * PHP 版本 在 Silva 代碼的基礎(chǔ)上修改的 
  3.  * 將URL地址轉(zhuǎn)化為完整的A標(biāo)簽鏈接代碼 
  4.  */ 
  5. /** ============================================= 
  6.  NAME        : replace_URLtolink() 
  7.  VERSION     : 1.0 
  8.  AUTHOR      : J de Silva 
  9.  DESCRIPTION : returns VOID; handles converting 
  10.  URLs into clickable links off a string. 
  11.  TYPE        : functions 
  12.  ============================================= */ 
  13.  
  14. function replace_URLtolink($text) { 
  15.     // grab anything that looks like a URL... 
  16.     $urls = array(); 
  17.      
  18.     // build the patterns 
  19.     $scheme = '(https?://|ftps?://)?'
  20.     $www = '([w]+.)'
  21.     $ip = '(d{1,3}.d{1,3}.d{1,3}.d{1,3})'
  22.     $name = '([w0-9]+)'
  23.     $tld = '(w{2,4})'
  24.     $port = '(:[0-9]+)?'
  25.     $the_rest = '(/?([w#!:.?+=&%@!-/]+))?'
  26.     $pattern = $scheme.'('.$ip.$port.'|'.$www.$name.$tld.$port.')'.$the_rest
  27.     $pattern = '/'.$pattern.'/is'
  28.      
  29.     // Get the URLs 
  30.     $c = preg_match_all($pattern$text$m); 
  31.      
  32.     if ($c) { 
  33.         $urls = $m[0]; 
  34.     } 
  35.      
  36.     // Replace all the URLs 
  37.     if (! emptyempty($urls)) { 
  38.         foreach ($urls as $url) { 
  39.             $pos = strpos('http://'$url); 
  40.              
  41.             if (($pos && $pos != 0) || !$pos) { 
  42.                 $fullurl = 'http://'.$url
  43.             } else { 
  44.                 $fullurl = $url
  45.             } 
  46.              
  47.             $link = ''.$url.''
  48.              
  49.             $text = str_replace($url$link$text); 
  50.         } 
  51.     } 
  52.      
  53.     return $text

例一測試過,例二沒有測試過大家測試一下看那個好用用那個吧.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 台中县| 奉节县| 神池县| 钟山县| 客服| 灵璧县| 青河县| 古蔺县| 松原市| 永清县| 商南县| 扶绥县| 赤水市| 闻喜县| 沧州市| 会昌县| 西充县| 万山特区| 九龙县| 布拖县| 屯昌县| 宜川县| 商南县| 荥阳市| 西藏| 仙桃市| 甘南县| 吉木萨尔县| 闽清县| 澳门| 砀山县| 五原县| 漳浦县| 鹿邑县| 子洲县| 都江堰市| 获嘉县| 汽车| 咸阳市| 夹江县| 灵璧县|