php converts the url address to the full a tag link code of php adds the a tag to the url address

  • 2020-12-10 00:39:03
  • OfStack

The contents to be extracted are as follows:


<a href="http://baidu.com">http://baidu.com</a> This is the first 1 a A The label, 
<a href="http://blog.baidu.com"> Growth footprints - Focus on Internet development </a> This is the first 2 a A The label. 
https://www.ofstack.com This is the first 1 One that needs to be extracted URL The address, 
http://blog.baidu.com This is the first 2 One that needs to be extracted URL address ' . 
<img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif"> , this is 1 a IMG The label 

Similar to the automatic extraction of URL in weibo hyperlink address. That is, the content is extracted, the A tag is added, and converted into a real hyperlink. After searching on the Internet for a long time, I couldn't find a practical solution. Most of them are just simple extraction of URL (the address in A tag and IMG tag are also extracted and replaced), which cannot meet the above requirements. There is also no way in the regular expression to filter out the A tag when extracting. So I changed one line of thought, "curve saving the country". In other words, all A tags and IMG tags are regularly replaced with the tags of one series 1, then the URL addresses are extracted and replaced with hyperlinks, and finally the tags of series 1 are restored and replaced with the previous A tags and IMG tags.


function linkAdd($content){
 // Extract and replace all of them A Label (series 1 tag <{link}> ) 
 preg_match_all('/<a.*?href=".*?".*?>.*?</a>/i',$content,$linkList);
 $linkList=$linkList[0];
 $str=preg_replace('/<a.*?href=".*?".*?>.*?</a>/i','<{link}>',$content);
 // Extract and replace all of them IMG Label (series 1 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="\0" target="_blank">\0</a>',$str);

 // reduction A system 1 Mark it as the original A The label 
 $arrLen=count($linkList);
 for($i=0;$i<$arrLen;$i++){
  $str=preg_replace('/<{link}>/',$linkList[$i],$str,1); 
 }

 // reduction IMG system 1 Mark it as the original IMG The label 
 $arrLen2=count($imgList);
 for($i=0;$i<$arrLen2;$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 1 a A The label, 
<a href="http://blog.baidu.com"> Growth footprints - Focus on Internet development </a> This is the first 2 a A The label. 
https://www.ofstack.com This is the first 1 One that needs to be extracted URL The address, 
http://blog.baidu.com This is the first 2 One that needs to be extracted URL Address. 
<img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif"> , this is 1 a IMG The label ';
echo linkAdd($content);

The returned content is:


<a href="http://baidu.com">http://baidu.com</a> This is the first 1 a A The label,  <a href="http://blog.baidu.com"> Growth footprints - Focus on Internet development </a> This is the first 2 a A The label.  <a href="https://www.ofstack.com" target="_blank">https://www.ofstack.com</a> This is the first 1 One that needs to be extracted URL The address,  <a href="http://blog.baidu.com" target="_blank">http://blog.baidu.com</a> This is the first 2 One that needs to be extracted URL Address. 
<img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif"> , this is 1 a IMG The label 

That's what we want.

Case 2,


/**
 * PHP  version   in  Silva  Modified on the basis of the code 
 *  will URL Address converted to 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 articles: