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

时间:2011-10-21 02:08:09

标签: php html tags replace

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

例如:

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

那么我怎么能这样做:

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

谢谢!

2 个答案:

答案 0 :(得分:1)

$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。

答案 1 :(得分:0)

这是我用来做的功能,我没有写它,请参阅源代码

    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\-._~!$&\'()*+,;=:]|%[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\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*?)*';

    $query = '(?:\?(?:[a-zA-Z0-9\-._~!$&\'()*+,;=:/?]|%[0-9A-Fa-f]{2})*?)?';
    $fragment = '(?:#(?:[a-zA-Z0-9\-._~!$&\'()*+,;=:/?]|%[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;

}