如何使用 PHP 将单词转换为字符串中的链接?

发布于 2024-12-11 06:31:55 字数 256 浏览 0 评论 0原文

如果我有一个段落包含纯 URL(没有锚点)。如何将这些 URL 封装在标签中?

例如:

$string = "visit http://blabla.com to get something";

那么我怎样才能做到这一点:

visit http://blabla.com得到一些东西

谢谢!

If I have a paragraph with contains plain URLs (without the anchors). How can I wrap those URLs within a tags?

For example:

$string = "visit http://blabla.com to get something";

So how could I make that as:

visit <a href="http://blabla.com">http://blabla.com</a> to get something

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪落纷纷 2024-12-18 06:31:55
$str = "so i found this sweet website, http://www.stackoverflow.com you should check it out";

$modified = preg_replace("@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@", "<a href='$1' target='_blank'>$1</a>", $str);

echo $modified;

或者在函数中

function linkUrls($str) {
    return preg_replace("@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@", "<a href='$1' target='_blank'>$1</a>", $str);
}
$str = "blah blah http://www.stackoverflow.com jaejg";
echo linkUrls($str);

您可以忽略 $1 。它被分配给与正则表达式匹配的文本。在本例中为 URL。

$str = "so i found this sweet website, http://www.stackoverflow.com you should check it out";

$modified = preg_replace("@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@", "<a href='$1' target='_blank'>$1</a>", $str);

echo $modified;

Or in a function

function linkUrls($str) {
    return preg_replace("@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@", "<a href='$1' target='_blank'>$1</a>", $str);
}
$str = "blah blah http://www.stackoverflow.com jaejg";
echo linkUrls($str);

You can ignore the $1 . It is assigned to the text matched by the regular expression. In this case, the URL.

浪荡不羁 2024-12-18 06:31:55

这是我用来执行此操作的函数,我没有编写它,请参阅源代码

    function urls2linksComplex($text,$schemes = 'normal',$tlds = 'normal'){

    //"urls2links - Complex" function by mBread @ SwirlDrop / m-bread web labs ( http://m-bread.com/lab/php/urls2linksComplex )
    //This functon is free to distribute and modify, but please leave these comments intact.
    if($schemes=='normal'){
        $scheme = '(?:[Hh][Tt]|[Ff])[Tt][Pp][Ss]?';
    }elseif(is_array($schemes)){
        $scheme = '(?:'.implode('|',$schemes).')';
    }elseif(is_string($schemes)){
        $scheme = $schemes;
    }else{
        $scheme = '[a-zA-Z][a-zA-Z0-9\-+.]*';
    }
    ; //EoIF


    if($tlds=='normal'){
        $tldExclude = array(
            'doc','xls','txt','rtf','jpeg','jpg','gif','png','exe','html','htm','zip','gz','scr','rar','php','php3','inc','ico','bmp','asp','jsp','dat','lnk','cab','csv','xml','xsl','xsd','svg','psp','psd','pdf','bak','wav','mp3','m4v','midi','wmv','wma','js','css','ppt','pps','mdb');
    }elseif(is_array($tlds)){
        $tldExclude = $tlds;
    }elseif(is_string($tlds)){
        $tldExclude = array(
            $tlds);
    }else{
        $tldExclude = array();
    }
    ; //EoIF
    $userinfo = '(?:(?:[a-zA-Z0-9\-._~!
amp;\'()*+,;=:]|%[0-9A-Fa-f]{2})*@)?';

    $decOctet = '(?:[0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
    $ipv4 = '(?:'.$decOctet.'\.){3}'.$decOctet;
    $regname = '(?:(?:[0-9A-Za-z][0-9A-Za-z\-]*[0-9A-Za-z]|[0-9A-Za-z])\.)+[a-zA-Z]{2,6}';
    $host = '('.$ipv4.'|'.$regname.')';
    $port = '(?::[0-9]*)?';
    $authority = '((?://)?'.$userinfo.$host.$port.')';

    $path = '(?:/(?:[a-zA-Z0-9\-._~!
amp;\'()*+,;=:]|%[0-9A-Fa-f]{2})*?)*';

    $query = '(?:\?(?:[a-zA-Z0-9\-._~!
amp;\'()*+,;=:/?]|%[0-9A-Fa-f]{2})*?)?';
    $fragment = '(?:#(?:[a-zA-Z0-9\-._~!
amp;\'()*+,;=:/?]|%[0-9A-Fa-f]{2})*?)?';
    $pattern = '\b(('.$scheme.'\:)?'.$authority.$path.$query.$fragment.')($|[^\w/][<\s]|[<\s]|[^\w/]$)';
    $replacement = '( !in_array( substr(\'$4\', strrpos(\'$4\', \'.\')+1), $tldExclude) )?\'<a href="\'.((\'$2\' == \'\')?((strpos(\'$3\', \'@\'))?\'mailto:$1\':\'http://$1\'):\'$1\').\'">$1</a>$5\':\'$0\'';

    //$pattern=strip_tags($pattern);


    $text = preg_replace('`'.$pattern.'`e',$replacement,$text);
    $text = preg_replace('`<a href="(.*)"><a href="(.*)">`','<a href="$1">',$text);
    $text = str_replace('</a></a>','</a>',$text);

    return $text;

}

this is the function i use to do that, i did not write it, see code for source

    function urls2linksComplex($text,$schemes = 'normal',$tlds = 'normal'){

    //"urls2links - Complex" function by mBread @ SwirlDrop / m-bread web labs ( http://m-bread.com/lab/php/urls2linksComplex )
    //This functon is free to distribute and modify, but please leave these comments intact.
    if($schemes=='normal'){
        $scheme = '(?:[Hh][Tt]|[Ff])[Tt][Pp][Ss]?';
    }elseif(is_array($schemes)){
        $scheme = '(?:'.implode('|',$schemes).')';
    }elseif(is_string($schemes)){
        $scheme = $schemes;
    }else{
        $scheme = '[a-zA-Z][a-zA-Z0-9\-+.]*';
    }
    ; //EoIF


    if($tlds=='normal'){
        $tldExclude = array(
            'doc','xls','txt','rtf','jpeg','jpg','gif','png','exe','html','htm','zip','gz','scr','rar','php','php3','inc','ico','bmp','asp','jsp','dat','lnk','cab','csv','xml','xsl','xsd','svg','psp','psd','pdf','bak','wav','mp3','m4v','midi','wmv','wma','js','css','ppt','pps','mdb');
    }elseif(is_array($tlds)){
        $tldExclude = $tlds;
    }elseif(is_string($tlds)){
        $tldExclude = array(
            $tlds);
    }else{
        $tldExclude = array();
    }
    ; //EoIF
    $userinfo = '(?:(?:[a-zA-Z0-9\-._~!
amp;\'()*+,;=:]|%[0-9A-Fa-f]{2})*@)?';

    $decOctet = '(?:[0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
    $ipv4 = '(?:'.$decOctet.'\.){3}'.$decOctet;
    $regname = '(?:(?:[0-9A-Za-z][0-9A-Za-z\-]*[0-9A-Za-z]|[0-9A-Za-z])\.)+[a-zA-Z]{2,6}';
    $host = '('.$ipv4.'|'.$regname.')';
    $port = '(?::[0-9]*)?';
    $authority = '((?://)?'.$userinfo.$host.$port.')';

    $path = '(?:/(?:[a-zA-Z0-9\-._~!
amp;\'()*+,;=:]|%[0-9A-Fa-f]{2})*?)*';

    $query = '(?:\?(?:[a-zA-Z0-9\-._~!
amp;\'()*+,;=:/?]|%[0-9A-Fa-f]{2})*?)?';
    $fragment = '(?:#(?:[a-zA-Z0-9\-._~!
amp;\'()*+,;=:/?]|%[0-9A-Fa-f]{2})*?)?';
    $pattern = '\b(('.$scheme.'\:)?'.$authority.$path.$query.$fragment.')($|[^\w/][<\s]|[<\s]|[^\w/]$)';
    $replacement = '( !in_array( substr(\'$4\', strrpos(\'$4\', \'.\')+1), $tldExclude) )?\'<a href="\'.((\'$2\' == \'\')?((strpos(\'$3\', \'@\'))?\'mailto:$1\':\'http://$1\'):\'$1\').\'">$1</a>$5\':\'$0\'';

    //$pattern=strip_tags($pattern);


    $text = preg_replace('`'.$pattern.'`e',$replacement,$text);
    $text = preg_replace('`<a href="(.*)"><a href="(.*)">`','<a href="$1">',$text);
    $text = str_replace('</a></a>','</a>',$text);

    return $text;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文