在文本块中单击链接的最佳方法

时间:2011-03-17 15:23:11

标签: php regex preg-replace

我想:

Here is link: http://google.com
And http://example.com inside.
And another one at the very end: http://test.net

成为:

Here is link: <a href="http://google.com">http://google.com</a>
And <a href="http://example.com">http://example.com</a> inside.
And another one at the very end: <a href="http://test.net">http://test.net</a>

看起来像一个简单的任务,但我找不到有效的PHP函数。你有什么想法吗?

function make_links_clickable($text){
    // ???
}

$text = 'Here is link: http://google.com
And http://example.com inside.
And another one at the very end: http://test.net';

echo make_links_clickable($text);

6 个答案:

答案 0 :(得分:48)

使用此功能(适用于ftp,http,ftps和https方案):

function make_links_clickable($text){
    return preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1">$1</a>', $text);
}

答案 1 :(得分:6)

尝试这样的事情:

function make_links_clickable($text)
{
   return preg_replace ('/http:\/\/[^\s]+/i', "<a href=\"${0}\">${0}</a>", $text);
} 

$result = make_links_clickable($text);

答案 2 :(得分:5)

你应该参考这个答案Replace URLs in text with HTML links

答案 3 :(得分:5)

function makeClickableLinks($text)
{
$text = html_entity_decode($text);
$text = " ".$text;
$text= preg_replace("/(^|[\n ])([\w]*?)([\w]*?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" >$3</a>", $text);  
$text= preg_replace("/(^|[\n ])([\w]*?)((www|wap)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" >$3</a>", $text);
$text= preg_replace("/(^|[\n ])([\w]*?)((ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"$4://$3\" >$3</a>", $text);  
$text= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\">$2@$3</a>", $text);  
$text= preg_replace("/(^|[\n ])(mailto:[a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"$2@$3\">$2@$3</a>", $text);  
$text= preg_replace("/(^|[\n ])(skype:[^ \,\"\t\n\r<]*)/i", "$1<a href=\"$2\">$2</a>", $text);  
        return $text;
}

使用:

www.example.com

https://www.example.com

http://www.example.com

wap.example.com

ftp.example.com

user@example.com

SKYPE:例如

的mailto:user@example.com

atherprotocol://示例.com

答案 4 :(得分:2)

受到Akarun回答的启发,我提出了tis函数来处理仅以www.开头的所有协议和链接

function make_links($text, $class='', $target='_blank'){
    return preg_replace('!((http\:\/\/|ftp\:\/\/|https\:\/\/)|www\.)([-a-zA-Zа-яА-Я0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?!ism', 
    '<a class="'.$class.'" href="//$3" target="'.$target.'">$1$3</a>', 
    $text);
}

此函数具有可选参数,用于将类名添加到链接以及链接的可选目标,因此它们在新窗口/选项卡上打开...默认情况下,参数会打开指向新窗口/选项卡的链接,但如果您觉得如果不这样做,您可以更改默认值,或在调用函数时更改值。

答案 5 :(得分:2)

同样受到Akarun的回答的启发,以下功能将转向仅链接尚未链接的文本。添加的功能是检查目标字符串中是否已存在与捕获的文本链接的链接:

function make_links_from_http($content) {   
    // Links out of text links
    preg_match_all('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', $content, $matches);
    foreach ($matches[0] as $key=>$link) {
        if (!preg_match('!<a(.*)'.$link.'(.*)/a>!i', $content))
        {
            $content = str_replace($link, '<a href="'.$link.'" target="_blank">'.$link.'</a>', $content);
        }
    }

    return $content;
} 

通过测试,我注意到上述功能在第5行失败。 A&#34;混乱&#34;完成工作的功能如下:

function make_links_from_http($content) 
{
    // The link list
    $links = array();

    // Links out of text links
    preg_match_all('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', $content, $matches);
    foreach ($matches[0] as $key=>$link) 
    {
        $links[$link] = $link;
    }

    // Get existing
    preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $content, $matches);
    foreach ($matches[2] as $key=>$value)
    {
        if (isset($links[$value]))
        {
            unset($links[$value]);
        }
    }

    // Replace in content
    foreach ($links as $key=>$link)
    {
        $content = str_replace($link, '<a href="'.$link.'" target="_blank">'.$link.'</a>', $content);
    }

    return $content;
} 

对于新代码,我使用了以下教程: http://www.the-art-of-web.com/php/parse-links/