PHP translates URL address into full a tag link code (PHP adds a tag to URL address) _php instance

Source: Internet
Author: User
Tags tld

The contents to be extracted are as follows:

Copy Code 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 tag.
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.

Similar to the microblogging in the automatic extraction URL is a hyperlink address. 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.

Copy Code code as follows:

function Linkadd ($content) {
Extract replaces all a tags (uniform <{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 (uniform markup <{img}>)
 preg_match_all ('/]+>/im ', $content, $ Imglist);
  $imgList = $imgList [0];
  $str =preg_replace ('/]+>/im ', ' <{img}> ', $str);

 //to extract the replacement standard URL address
  $str =preg_replace (((f|ht) {1}tp://) [-a-za-z0-9@:%_/+.~#?&//=]+) ', ' <a href= "target=" _blank ">\0</a>", $str);

 //Restore A is uniformly marked as the original a label
  $arrLen =count ($linkList);
 for ($i =0; $i < $arrLen; $i + +) {
  $str =preg_replace ('/<{link}>/', $linkList [$i], $STR, 1);
 }

 //restore IMG Unified mark for the original IMG label
  $arrLen 2=count ($imgList);
 for ($i =0; $i < $arrLen 2; $i + +) {
  $str =preg_replace ('/<{img}>/', $imgList [$i], $STR, 1);
 }

 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 tag.
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.
echo Linkadd ($content);


The returned contents are:

Copy Code 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. <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.

That's what we want.

Example 2,

Copy Code code as follows:

/**
* PHP version modified on the basis of the Silva code
* Convert URL address to full A-tag link code
*/

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 URLs
if (! empty ($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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.