1

可能重复:
php正则表达式匹配html标签之外

我在这里找到了一个不错的功能: https ://stackoverflow.com/a/1945957

它将文本 URL 转换为正确的链接,但它也匹配标签中的 URL,例如<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) . "&hellip;";
       }

       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]|/)))(?=[^>]*(<|$))#
4

0 回答 0