开发者

Best way to make links clickable in block of text [duplicate]

This question already has answers here: Replace URLs in text with HTML links (17 answers) Closed 2 years ago.

I want:

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

to become:

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 hre开发者_如何转开发f="http://test.net">http://test.net</a>

Seems like a trivial task, but I cannot find a PHP function that works. Do you have any ideas?

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);


Use this (works with ftp, http, ftps and https schemes):

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


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;
}

work with:

www.example.com

https://www.example.com

http://www.example.com

wap.example.com

ftp.example.com

user@example.com

skype:example

mailto:user@example.com

atherprotocol://example.com


Try something like this:

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

$result = make_links_clickable($text);


Also inspired by Akarun's answer, the following function will turn to links only the text that is not already a link. The added functionality is checking that a link with the captured text link doesn't already exists in the target string:

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;
} 

By testing, I noticed that the above function fails on line #5. A "messier" function that does the job is the following:

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;
} 

For the new code, I have used the tutorial at: http://www.the-art-of-web.com/php/parse-links/


Inspired by Akarun's answer I came up with tis function to handle all protocols and links that start with only 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);
}

This function has optional parameters to add class names onto the links and also optional target for the link, so they open on new window/tab... by default the parameter opens links to new window/tab, but if you feel like not doing that, you can change default, or change the value when calling the function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜