Php 要匹配的正则表达式和<;a href>;?

Php 要匹配的正则表达式和<;a href>;?,php,regex,Php,Regex,我在上找到了此代码,以便将文本链接更改为超链接: function auto_link_text($text) { $pattern = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#'; $callback = create_function('$matches', ' $url = array_shift($matches); $url_parts

我在上找到了此代码,以便将文本链接更改为超链接:

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="nowfollow" href="%s">%s</a>\', $url, $text);
  ');

  return preg_replace_callback($pattern, $callback, $text);
}
功能自动链接文本($text)
{ 
$pattern='#\b(([\w-]+:/?| www[.])[^\s()]+(?:\([\w\d]+\)|([^[:punct:][\s]|/)));
$callback=create_函数('$matches','
$url=数组移位($matches);
$url\u parts=parse\u url($url);
$text=parse\uURL($url,PHP\uURL\uHost)。parse\uURL($url,PHP\uURL\uPath);
$text=preg_replace(“/^www./”,“”,$text);
$last=-(strlen(strrchr($text,“/”))+1;
如果($last<0){
$text=substr($text,0,$last)。“&hellip;”;
}
返回sprintf(\'\',$url,$text);
');
返回preg_replace_回调($pattern,$callback,$text);
}
但是,即使它们已经是超链接的一部分,它似乎也会将它们更改为超链接,因此您最终会得到类似于“
\”、$url、$text)的内容;
');
返回preg_replace_回调($pattern,$callback,$text);
}
}

或者可能函数中的正则表达式应该更改。

添加
(?Check out my的可能重复项。这只检查它是否直接前面有href,但它不会检查它是否在它的某个位置或它是否在文本节点中。嵌套链接是一个坏主意。如果php支持通配符lookbehinds,这将起作用,但它不起作用。lookbehinds必须是预定义的长度。呃…这确保rl前面没有
href=“
”,这意味着它不在
href
属性中。您可以更粗暴地使用
(?。我自己的代码只有
,因为输入中的
的真实实例已经转换为
@Kolink我添加了它并输入了
,它仍然中断了链接。
  function auto_link_text($text)
{
  if preg_match(proper stuff in here){
      return $text;
        }else{
       $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="nowfollow" href="%s">%s</a>\', $url, $text);
  ');

  return preg_replace_callback($pattern, $callback, $text);
}
}