PHP converts the URL address into a full A-tag link code (PHP adds a tag to the URL address) _php tutorial

Source: Internet
Author: User
Tags tld
What you need to extract is as follows:

Copy the Code code as follows:
Http://baidu.com This is the first a-label,
Growth footprints-focus on Internet development 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.
, this is an IMG tag

An auto-fetch URL in a similar microblog is a hyperlink address. That is, the content is extracted to add a tag, converted into a real hyperlink. Search the Internet for a long time, did not find a practical solution. Most are simply extracted URLs (a tag and the address within the IMG tag are also extracted and replaced) and do not meet the above requirements. It is also not found in regular expressions to be able to filter out a tag when extracting. So converted a bit of thought, "curve to the salvation." That is, all the A and IMG tags are replaced with a single uniform tag, and then the URL is replaced with a hyperlink, and finally the unified markup restore is replaced with the previous a tag and the IMG tag to resolve.

Copy the Code code as follows:
function Linkadd ($content) {
Extract replaces all a tags (unified tagging <{link}>)
Preg_match_all ('/.*?/i ', $content, $linkList);
$linkList = $linkList [0];
$str =preg_replace ('/.*?/i ', ' <{link}> ', $content);

Extract replaces all IMG tags (unified tagging <{img}>)
Preg_match_all ('/]+>/im ', $content, $imgList);
$imgList = $imgList [0];
$str =preg_replace ('/]+>/im ', ' <{img}> ', $str);

Extract the URL address of the replacement standard
$str =preg_replace (' ((f|ht) {1}tp://) [-a-za-z0-9@:%_/+.~#?&//=]+] ', ' + ', $str);

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

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

return $str;
}

$content = '
Http://baidu.com This is the first a-label,
Growth footprints-focus on Internet development 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.
, which is an IMG tag ';
echo Linkadd ($content);

The returned content is:

Copy the Code code as follows:
Http://baidu.com This is the first a-label, growth footprint-focus on Internet development 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 that needs to be extracted.
, this is an IMG tag

That's what we want.

Example 2,
Copy the 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 the 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;
}

http://www.bkjia.com/PHPjc/710607.html www.bkjia.com true http://www.bkjia.com/PHPjc/710607.html techarticle What you need to extract is as follows: Copy the 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 footprints-focus on mutual ...

  • 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.