简体   繁体   English

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

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

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 href="http://test.net">http://test.net</a> 

Seems like a trivial task, but I cannot find a PHP function that works. 看起来像一个简单的任务,但我找不到一个有效的PHP函数。 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): 使用此方法(适用于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);
}

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);
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 www.example.com

https://www.example.com https://www.example.com

http://www.example.com http://www.example.com

wap.example.com wap.example.com

ftp.example.com ftp.example.com

user@example.com user@example.com

skype:example SKYPE:例如

mailto:user@example.com 邮寄地址:user@example.com

atherprotocol://example.com atherprotocol://示例.com

你应该参考这个答案用HTML链接替换文本中的URL

Inspired by Akarun's answer I came up with tis function to handle all protocols and links that start with only www. 在Akarun的回答的启发下,我提出了tis函数来处理所有仅以www.开头的协议和链接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. 此函数具有可选参数,用于将类名添加到链接上,也可以是链接的可选目标,因此它们在新窗口/选项卡上打开...默认情况下,参数会打开指向新窗口/选项卡的链接,但如果您不想这样做,您可以更改默认值,或在调用函数时更改值。

Also inspired by Akarun's answer, the following function will turn to links only the text that is not already a link. 同样受Akarun答案的启发,以下功能将转向仅链接尚未链接的文本。 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. 通过测试,我注意到上面的函数在第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/ 对于新代码,我使用了以下教程: http//www.the-art-of-web.com/php/parse-links/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM