加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP – 添加链接到字符串中的URL

发布时间:2020-12-13 14:06:39 所属栏目:PHP教程 来源:网络整理
导读:我有一个功能,将添加 a href链接前的标签和 / a之后的链接.但是,对于某些网页而言,它会中断.你如何改善这个功能?谢谢! function processString($s) { // check if there is a link if(preg_match("/http:///",$s)) { print preg_match("/http:///",$s)
我有一个功能,将添加< a href>链接前的标签和< / a>之后的链接.但是,对于某些网页而言,它会中断.你如何改善这个功能?谢谢!
function processString($s) 
{
    // check if there is a link

    if(preg_match("/http:///",$s))
    {
        print preg_match("/http:///",$s);


        $startUrl =  stripos($s,"http://");

        // if the link is in between text
        if(stripos($s," ",$startUrl)){
            $endUrl = stripos($s,$startUrl);
        }
        // if link is at the end of string
        else {$endUrl = strlen($s);}

        $beforeUrl = substr($s,$startUrl);
        $url = substr($s,$startUrl,$endUrl-$startUrl);
        $afterUrl = substr($s,$endUrl);

        $newString = $beforeUrl."<a href="$url">".$url."</a>".$afterUrl;

        return $newString;
    }

    return $s;
}
function processString($s) {
    return preg_replace('/https?://[w-.!~#?&=+*'"(),/]+/','<a href="$0">$0</a>',$s);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读