查找字符串中的URL并转换为链接

问题描述 投票:0回答:2

我正在使用the code given on this page浏览字符串并将URL转换为HTML链接。

效果很好,但是其中的“替换”部分有一点问题。

当我拥有几乎相同的链接时,就会出现问题。例如:

https://example.com/page.php?goto=200https://example.com/page.php

第一个链接一切正常,但是第二个链接将在第一个<a>标签中创建一个<a>标签。

首次运行

<a href="https://example.com/page.php?goto=200">https://example.com/page.php?goto=200</a>

第二

<a href="<a href="https://example.com/page.php">https://example.com/page.php</a>?goto=200"><a href="https://example.com/page.php">https://example.com/page.php</a>?goto=200</a>

因为它还将替换刚刚创建的html链接。

如何避免这种情况?

<?php

function turnUrlIntoHyperlink($string){

    //The Regular Expression filter
    $reg_exUrl = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";

    // Check if there is a url in the text
    if(preg_match_all($reg_exUrl, $string, $url)) {

        // Loop through all matches
        foreach($url[0] as $newLinks){
            if(strstr( $newLinks, ":" ) === false){
                $link = 'http://'.$newLinks;
            }else{
                $link = $newLinks;
            }

            // Create Search and Replace strings
            $search  = $newLinks;
            $replace = '<a href="'.$link.'" title="'.$newLinks.'" target="_blank">'.$link.'</a>';
            $string = str_replace($search, $replace, $string);
}
    }

    //Return result
    return $string;
}
?>
php replace
2个回答
0
投票

您首先需要在正则表达式中添加空格标识符\s,并删除\b,因为\b仅返回最后一个匹配项。您的正则表达式可以写成:

$reg_exUrl = "/(?i)\s((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/"

选中此一项:https://regex101.com/r/YFQPlZ/1


0
投票

由于我无法使用建议的正则表达式,因此我对替换部分进行了一些更改。

也许可以做得更好,但我仍在学习:)

function turnUrlIntoHyperlink($string){

    //The Regular Expression filter
    $reg_exUrl = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";

    // Check if there is a url in the text
    if(preg_match_all($reg_exUrl, $string, $url)) {

        // Loop through all matches
        foreach($url[0] as $key => $newLinks){

            if(strstr( $newLinks, ":" ) === false){
                $url = 'https://'.$newLinks;
            }else{
                $url = $newLinks;
            }

            // Create Search and Replace strings
            $replace .= '<a href="'.$url.'" title="'.$url.'" target="_blank" class="external">'.$url.'</a>,';
            $newLinks = '/'.preg_quote($newLinks, '/').'/';
            $string = preg_replace($newLinks, '{'.$key.'}', $string, 1);

        }
        $arr_replace = explode(',', $replace);
        foreach ($arr_replace as $key => $link) {
            $string = str_replace('{'.$key.'}', $link, $string);
        }
    }

    //Return result
    return $string;
}
© www.soinside.com 2019 - 2024. All rights reserved.