我在这里发现了一个很好的功能: https://stackoverflow.com/a/1945957
它会将文本网址转换为正确的链接,但它也会匹配<img>
等标记中的网址。是否可以修改函数以仅匹配不在引号(单引号或双引号)的URL?
由于
/**
* Replace links in text with html links
*
* @param string $text
* @return string
*/
function auto_link_text($text)
{
$pattern = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
$callback = create_function('$matches', '
$url = array_shift($matches);
$url_parts = parse_url($url);
$text = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
$text = preg_replace("/^www./", "", $text);
$last = -(strlen(strrchr($text, "/"))) + 1;
if ($last < 0) {
$text = substr($text, 0, $last) . "…";
}
return sprintf(\'<a rel="nofollow" href="%s">%s</a>\', $url, $text);
');
return preg_replace_callback($pattern, $callback, $text);
}
输入:
<img src = "http://www.google.com/logo.png" /> http://www.google.com
预期产出:
<img src = "http://www.google.com/logo.png" /> <a rel="nofollow" href="http://www.google.com">http://www.google.com</a>
解决:
#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))(?=[^>]*(<|$))#