[英]Best way to make links clickable in block of text
我想要:
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);
使用此方法(適用於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);
}
嘗試這樣的事情:
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;
}
合作:
www.example.com
wap.example.com
ftp.example.com
user@example.com
SKYPE:例如
郵寄地址:user@example.com
atherprotocol://示例.com
你應該參考這個答案用HTML鏈接替換文本中的URL
在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);
}
此函數具有可選參數,用於將類名添加到鏈接上,也可以是鏈接的可選目標,因此它們在新窗口/選項卡上打開...默認情況下,參數會打開指向新窗口/選項卡的鏈接,但如果您不想這樣做,您可以更改默認值,或在調用函數時更改值。
同樣受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行失敗了。 執行該任務的“雜亂”功能如下:
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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.