【发布时间】:2013-07-25 10:06:56
【问题描述】:
function makeLinksInTheContent($html)
{
$html= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" rel=\"nofollow\" >$3</a>", $html);
$html= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" rel=\"nofollow\" >$3</a>", $html);
$html= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\" rel=\"nofollow\">$2@$3</a>", $html);
return($html);
}
这是我的代码。
我需要自动链接网址。使用 preg_replace 查找 url 并设置指向该 url 的链接。
例如:“一个页面包含 www.google.com。”如果我将此内容传递给 makeLinksInTheContent($html),它将返回“A page contains www.google.com.”
但以下网址格式未链接。
www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?>
http://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?>
https://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?>
ftp://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?>
我认为我的正则表达式有一些错误。请建议我们。
【问题讨论】:
-
我已经回答过这个问题了...?这个确切的问题
标签: php regex preg-replace