Php converts the url address to the complete a tag link code (php adds a tag for the url address)

Source: Internet
Author: User
This article describes how to add a tag for a url address in php. for details, refer to the following content:

The code is as follows:
Http://baidu.com this is the first A tag,
Growth footprint-focusing on Internet development is the second A tag.
Http://www.bitsCN.com this is the first URL to be extracted,
Http://blog.baidu.com this is the second URL address to be extracted '.
This is an IMG tag

For example, the URL automatically extracted from Weibo is a hyperlink address. That is, extract the content and add the tag, and convert it into A real hyperlink. I searched the internet for a long time and did not find a feasible solution. Most of them are simply extracted URLs (the addresses in tags A and IMG are also extracted and replaced) and cannot meet the above requirements. The regular expression does not find any way to filter out the tag during extraction. So I switched my mind to "saving the nation through the curve ". That is, replace all the and IMG tags with A unified regex, and then extract the URL address and replace it with A hyperlink, finally, replace the unified Mark restoration with the previous A and IMG labels.

The code is as follows:
Function linkAdd ($ content ){
// Extract and replace all A Tags (unified tag <{link}>)
Preg_match_all ('/.*? /I ', $ content, $ linkList );
$ LinkList = $ linkList [0];
$ Str = preg_replace ('/.*? /I ',' <{link}> ', $ content );

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

// Extract and replace the standard URL address
$ Str = preg_replace ('(f | ht) {1} tp: //) [-a-zA-Z0-9 @: % _/+ .~ #? & // =] +) ',' \ 0', $ str );

// Restore the uniform A tag to the original A tag
$ ArrLen = count ($ linkList );
For ($ I = 0; $ I <$ arrLen; $ I ++ ){
$ Str = preg_replace ('/<{link}>/', $ linkList [$ I], $ str, 1 );
}

// Restore the IMG tag to the original IMG tag
$ ArrLen2 = count ($ imgList );
For ($ I = 0; $ I <$ arrLen2; $ I ++ ){
$ Str = preg_replace ('/<{img}>/', $ imgList [$ I], $ str, 1 );
}

Return $ str;
}

$ Content ='
Http://baidu.com this is the first A tag,
Growth footprint-focusing on Internet development is the second A tag.
Http://www.bitsCN.com this is the first URL to be extracted,
Http://blog.baidu.com this is the second URL to be extracted.
, This is an IMG tag ';
Echo linkAdd ($ content );

The returned content is:

The code is as follows:
Http://baidu.com is the first A tag, growth footprint-focus on the development of the Internet this is the second A tag. Http://www.bitsCN.com this is the first URL address to be extracted, http://blog.baidu.com this is the second URL address to be extracted.
This is an IMG tag

That is, what we want.

Example 2,
The code is as follows:
/**
* The PHP version is modified based on the Silva code
* Convert the URL address to the complete 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;
}

Related Article

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.