php如何将url地址转化为完整的a标签链接

发布时间:2021-08-25 08:13:19 作者:chen
来源:亿速云 阅读:138

这篇文章主要介绍“php如何将url地址转化为完整的a标签链接”,在日常操作中,相信很多人在php如何将url地址转化为完整的a标签链接问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”php如何将url地址转化为完整的a标签链接”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

复制代码 代码如下:


<a href="http://baidu.com">http://baidu.com</a>这是第一个A标签,
<a href="http://blog.baidu.com">成长脚印-专注于互联网发展</a>这是第二个A标签。
https://www.jb51.net这是第一个需要被提取的URL地址,
http://blog.baidu.com这是第二个需要被提取的URL地址'。
<img border="0" alt="" src="https://cache.yisu.com/upload/information/20201209/266/43022.gif">,这是一个IMG标签

类似微博中的自动提取URL为超链接地址。即内容提取出来添加A标签,转换成真正的超链接。网上搜索了很久,没有找到一个切实可行的解决方案。大都只是简单的提取URL(A标签和IMG标签内的地址也被提取替换了),并不能满足以上需求。正则表达式中也没发现能够实现提取时过滤掉A标签的方法。于是转换了一下思路,“曲线救国”。即,先将所有的A标签和IMG标签正则替换为某一个统一的标记,然后再提取URL地址替换为超链接,最后再将统一的标记还原替换为以前的A标签和IMG标签便解决了。

复制代码 代码如下:


function linkAdd($content){
 //提取替换出所有A标签(统一标记<{link}>)
 preg_match_all('/<a.*?href=".*?".*?>.*?</a>/i',$content,$linkList);
 $linkList=$linkList[0];
 $str=preg_replace('/<a.*?href=".*?".*?>.*?</a>/i','<{link}>',$content);

 //提取替换出所有的IMG标签(统一标记<{img}>)
 preg_match_all('/<img[^>]+>/im',$content,$imgList);
 $imgList=$imgList[0];
 $str=preg_replace('/<img[^>]+>/im','<{img}>',$str);

 //提取替换标准的URL地址
 $str=preg_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)','<a href="\0" target="_blank">\0</a>',$str);

 //还原A统一标记为原来的A标签
 $arrLen=count($linkList);
 for($i=0;$i<$arrLen;$i++){
  $str=preg_replace('/<{link}>/',$linkList[$i],$str,1);
 }

 //还原IMG统一标记为原来的IMG标签
 $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>这是第一个A标签,
<a href="http://blog.baidu.com">成长脚印-专注于互联网发展</a>这是第二个A标签。
https://www.jb51.net这是第一个需要被提取的URL地址,
http://blog.baidu.com这是第二个需要被提取的URL地址。
<img border="0" alt="" src="https://cache.yisu.com/upload/information/20201209/266/43022.gif">,这是一个IMG标签';
echo linkAdd($content);

返回的内容为:

复制代码 代码如下:


<a href="http://baidu.com">http://baidu.com</a>这是第一个A标签, <a href="http://blog.baidu.com">成长脚印-专注于互联网发展</a>这是第二个A标签。 <a href="https://www.jb51.net" target="_blank">https://www.jb51.net</a>这是第一个需要被提取的URL地址, <a href="http://blog.baidu.com" target="_blank">http://blog.baidu.com</a>这是第二个需要被提取的URL地址。
<img border="0" alt="" src="https://cache.yisu.com/upload/information/20201209/266/43022.gif">,这是一个IMG标签

即为我们想要的内容。

例2,

复制代码 代码如下:


/**
 * PHP 版本 在 Silva 代码的基础上修改的
 * 将URL地址转化为完整的A标签链接代码
 */

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

到此,关于“php如何将url地址转化为完整的a标签链接”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. PHP正则获取A标签的链接地址
  2. php获取url地址的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:PHP中的重载是什么意思

下一篇:wmiprvse是什么进程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》