php如何匹配成对注释?

解决方案 »

  1.   

    正则不好,前后呼应那样的不会,想个笨法子,不知是否适合,不适合就算了
    $x = 'b';
    $s = '<!-- '.$x.' -->aaa<!-- /'.$x.' -->';
    $r = '/<!-- '.$x.' -->.*<!-- \/'.$x.' -->/';
    preg_match_all($r, $s, $arr);
    echo $arr[0][0];
    //查看网页源代码看效果
      

  2.   

    $p = '#<!--(\w+)-->.*<!--/\1-->#is';
    $html = '<!--hello-->good morning<!--/hello--><!--baby-->laji<!--/ok-->';
    preg_match_all($p,$html,$m);
    print_r($m);
      

  3.   

    2楼正解
    一般写作
    '#<!--(\w+)-->.*<!--/\\1-->#is'随便找个模板引擎就可知道如何写了