php 在文本块中使链接可点击的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5341168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to make links clickable in block of text
提问by Silver Light
I want:
我想要:
Here is link: http://google.com And http://example.com inside. And another one at the very end: http://test.net
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>
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. Do you have any ideas?
似乎是一项微不足道的任务,但我找不到有效的 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);
回答by Akarun
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=""></a>', $text);
}
回答by Marco Demaio
Try something like this:
尝试这样的事情:
function make_links_clickable($text)
{
return preg_replace ('/http:\/\/[^\s]+/i', "<a href=\"function makeClickableLinks($text)
{
$text = html_entity_decode($text);
$text = " ".$text;
$text= preg_replace("/(^|[\n ])([\w]*?)([\w]*?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "<a href=\"\" ></a>", $text);
$text= preg_replace("/(^|[\n ])([\w]*?)((www|wap)\.[^ \,\"\t\n\r<]*)/is", "<a href=\"http://\" ></a>", $text);
$text= preg_replace("/(^|[\n ])([\w]*?)((ftp)\.[^ \,\"\t\n\r<]*)/is", "<a href=\"://\" ></a>", $text);
$text= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "<a href=\"mailto:@\">@</a>", $text);
$text= preg_replace("/(^|[\n ])(mailto:[a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "<a href=\"@\">@</a>", $text);
$text= preg_replace("/(^|[\n ])(skype:[^ \,\"\t\n\r<]*)/i", "<a href=\"\"></a>", $text);
return $text;
}
\">function make_links($text, $class='', $target='_blank'){
return preg_replace('!((http\:\/\/|ftp\:\/\/|https\:\/\/)|www\.)([-a-zA-Zа-яА-Я0-9\~\!\@\#$\%\^\&\*\(\)_\-\=\+\\/\?\.\:\;\'\,]*)?!ism',
'<a class="'.$class.'" href="//" target="'.$target.'"></a>',
$text);
}
</a>", $text);
}
$result = make_links_clickable($text);
回答by Sanya Snex
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;
}
work with:
与:
www.example.com
www.example.com
wap.example.com
wap.example.com
ftp.example.com
ftp.example.com
用户@example.com
skype:example
Skype:示例
mailto:[email protected]
atherprotocol://example.com
atherprotocol://example.com
回答by Lawrence Cherone
you should refer to this answer Replace URLs in text with HTML links
你应该参考这个答案用 HTML 链接替换文本中的 URL
回答by EffectiX
Inspired by Akarun's answer I came up with tis function to handle all protocols and links that start with only www.
受到 Akarun 的回答的启发,我想出了 tis 函数来处理所有以 only 开头的协议和链接 www.
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;
}
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.
此函数具有可选参数,用于将类名添加到链接上,以及链接的可选目标,因此它们会在新窗口/选项卡上打开...默认情况下,该参数会打开指向新窗口/选项卡的链接,但如果您不想这样做也就是说,您可以更改默认值,或者在调用函数时更改值。
回答by Cristi Draghici
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:
同样受到 Akarun 回答的启发,以下函数将只链接还不是链接的文本。添加的功能是检查目标字符串中是否不存在带有捕获文本链接的链接:
##代码##By testing, I noticed that the above function fails on line #5. A "messier" function that does the job is the following:
通过测试,我注意到上述函数在第 5 行失败。完成这项工作的“混乱”功能如下:
##代码##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/