function URLhtml($url)
{
#-php后缀匹配-#
$pat="php";
$url02=ereg_replace($pat,"html","\\2");
#-php后缀匹配-#
return $url02;
} ob_end_clean();
ob_start();
include('index.php');
$length = ob_get_length();
$buffer = ob_get_contents();

$pat = '/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/i';
$buffer=preg_replace($pat,URLhtml("\\2"),$buffer);
echo $buffer;上面那段代码为什么只可以找得到URL切不会函数匹配???

解决方案 »

  1.   

    URLhtml先于preg_replace执行了,所以根本不会有正则匹配结果传入URLhtml
      

  2.   


    我见DZ论坛的ubb也用了这种替换模式,他用了class类...不过不是用不用类的问题,是不是哪一步错了??
      

  3.   

    用preg_replace_callback代替preg_replace吧,看一下手册上的例子:
    <?php
      // 此文本是用于 2002 年的,
      // 现在想使其能用于 2003 年
      $text = "April fools day is 04/01/2002\n";
      $text.= "Last christmas was 12/24/2001\n";  // 回调函数
      function next_year($matches) {
        // 通常:$matches[0] 是完整的匹配项
        // $matches[1] 是第一个括号中的子模式的匹配项
        // 以此类推
        return $matches[1].($matches[2]+1);
      }  echo preg_replace_callback(
                  "|(\d{2}/\d{2}/)(\d{4})|",
                  "next_year",
                  $text);  // 结果为:
      // April fools day is 04/01/2003
      // Last christmas was 12/24/2002
    ?>