Home > php教程 > PHP源码 > body text

php将URL地址转化为完整的A标签链接代码

WBOY
Release: 2016-06-08 17:22:48
Original
1193 people have browsed it

前面介绍过js把文本链接转换成A标签,现在看一个php的方法,这个是在 Silva 代码的基础上修改的,如果有不完善的地方大家可提出来。

<script>ec(2);</script>

需要提取的内容如下:

http://baidu.com这是第一个A标签,
成长脚印-专注于互联网发展这是第二个A标签。
http://www.111cn.net这是第一个需要被提取的URL地址,
http://blog.baidu.com这是第二个需要被提取的URL地址'。
,这是一个IMG标签

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

 代码如下 复制代码

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

 //提取替换出所有的IMG标签(统一标记)
 preg_match_all('/php将URL地址转化为完整的A标签链接代码]+>/im',$content,$imgList);
 $imgList=$imgList[0];
 $str=preg_replace('/php将URL地址转化为完整的A标签链接代码]+>/im','',$str);
 
 //提取替换标准的URL地址
 $str=preg_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)','\0',$str);
 
 //还原A统一标记为原来的A标签
 $arrLen=count($linkList);
 for($i=0;$i   $str=preg_replace('//',$linkList[$i],$str,1);
 }
 
 //还原IMG统一标记为原来的IMG标签
 $arrLen2=count($imgList);
 for($i=0;$i   $str=preg_replace('//',$imgList[$i],$str,1);
 }
 
 return $str;
}

$content='
http://baidu.com这是第一个A标签,
成长脚印-专注于互联网发展这是第二个A标签。
http://www.111cn.net这是第一个需要被提取的URL地址,
http://blog.baidu.com这是第二个需要被提取的URL地址。
,这是一个IMG标签';
echo linkAdd($content);


返回的内容为:


http://baidu.com这是第一个A标签, 成长脚印-专注于互联网发展这是第二个A标签。 http://www.111cn.net这是第一个需要被提取的URL地址, http://blog.baidu.com这是第二个需要被提取的URL地址。
,这是一个IMG标签

即为我们想要的内容。

例2,

 代码如下 复制代码

/**
 * PHP 版本 在 Silva 代码的基础上修改的
 * 将URL地址转化为完整的A标签链接代码
 */
/** =============================================
 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;
}

例一测试过,例二没有测试过大家测试一下看那个好用用那个吧。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!