The
needs to be extracted is as follows:
Code as follows: <a href= "http://baidu.com" >http://baidu.com</a> This is the first a label, <a href= "http:// Blog.baidu.com "> Growth footprint-focus on Internet development </a> This is the second a label. Http://www.jb51.net This is the first URL address that needs to be extracted, http://blog.baidu.com this is the second URL address that needs to be extracted. <img border= "0" alt= "" src= "error.html", which is an IMG tag similar to the automatic extraction URL for a Weibo address as a hyperlink. That is, the content is extracted to add a tag, converted into a real hyperlink. Search on the internet for a long time, did not find a practical solution. Most simply extract URLs (a tag and the address within the IMG tag are also extracted and replaced) and do not meet the above requirements. The regular expression also found no way to filter a label when extracting. So the transformation of the idea, "curve to save the nation." That is, all the A label and the IMG tag are replaced with a uniform tag, then the URL address is replaced with a hyperlink, and then the unified token restore is replaced with the previous a tag and the IMG tag. Code as follows: function Linkadd ($content) { //Extract replaces all a tags (unified tag <{link}>) preg_match_all ('/<a. *?href= ". *?". *?>.*?</a>/i ', $content, $linkList); $linkList = $linkList [0]; $str =preg_replace ('/<a.*?href= ". *?). *?>.*?</a>/i ', ' <{link}> ', $content); //extract Replaces all IMG tags (unified tag <{img}>) preg_match_all ('/<img[^>]+>/im ', $content, $imgList ); $imgList = $imgList [0]; $STR =preg_replace('/<img[^>]+>/im ', ' <{img}> ', $str); //extract the replacement standard URL address $str =preg_replace (((f|ht) {1}tp://) [-a-za-z0-9@:%_/+.~#?&//=]+) ', ' <a href= "target=" _blank "></a>", $str); //restore a unified mark for the original a label $arrLen =count ($linkList); for ($i =0; $i < $arrLen; $i + +) { $str =preg_replace ('/<{link}>/', $linkList [$i], $STR, 1); &NBSP} //restore IMG Unified markup for the original IMG tag $arrLen 2=count ($imgList); for ($i =0 $i < $arrLen 2; $i + +) { $str =preg_replace ('/<{img}>/', $imgList [$i], $STR, 1); &NBSP} return $str; } $content = ' <a href= ' http://baidu.com ' >http://baidu.com</a> This is the first a label, <a href= ' http:// Blog.baidu.com "> Growth footprint-focus on Internet development </a> This is the second a label. Http://www.jb51.net This is the first URL address that needs to be extracted, http://blog.baidu.com this is the second URL address that needs to be extracted. <img border= "0" alt= "" src= "error.html", which is an IMG tag '; echo Linkadd ($content); Return the content is: code as follows: <a href= "http://baidu.com" >http://baidu.com</a> This is the first a tag, <a href= "http://blog.baidu.com" > Growth footprint-focus on Internet development </a> This is the second a tag. <a href= "http://www.jb51.net" target= "_blank" >http://www.jb51.net</a> This is the first URL address that needs to be extracted, <a href= " Http://blog.baidu.com "target=" _blank ">http://blog.baidu.com</a> This is the second URL address that needs to be extracted. <img border= "0" alt= "" src= "error.html", this is an IMG tag That is what we want. Example 2, code as follows:/** * PHP version modified on the basis of the Silva code * URL address into the full a tag link code */ function Replace_ Urltolink ($text) { //grab anything that looks like a URL ... $urls = array (); &N Bsp 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 URL $c = Preg_match_all ($pattern, $text, $m); if ($c) { $urls = $m [0]; } //Replace All URLs if (! empty ($urls)) { foreach ($urls as $url) { &NBSP ; $pos = Strpos (' http://', $url); if ($pos && $pos!= 0) | | $pos) { & nbsp $fullurl = ' http://'. $url; } else { $fullurl = $url ; $link = '. $url. '; $text = Str_replace ($url, $link, $text); } &NBSp } return $text; }