Php converts the URL address to the complete A tag link code-PHP source code

Source: Internet
Author: User
Tags tld
I have previously introduced how to convert A text link to A tag in js. Now let's look at A php method. This is modified based on the Silva code. If there are any imperfections, you can refer to it. I have previously introduced how to convert A text link to A tag in js. Now let's look at A php method. This is modified based on the Silva code. If there are any imperfections, you can refer to it.

Script ec (2); script

The content to be extracted 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.111cn.net 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. Extract the content marked in red to 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.111cn.net 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:


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.111cn.net 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
*/
/** =================================================== ==========
NAME: replace_URLtolink ()
VERSION: 1.0
AUTHOR: J de Silva
DESCRIPTION: returns VOID; handles converting
URLs into clickable links off a string.
TYPE: functions
========================================================== ===== */

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;
}

Example 1: tested. Example 2: tested. Let's take a look at the useful method.

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.