代码之家  ›  专栏  ›  技术社区  ›  tic

preg_replace:未知修饰符

  •  6
  • tic  · 技术社区  · 16 年前

    假设$body等于

    something 
    that 
    does 
    not 
    interest 
    me 
    <!-- start -->
    some
    html
    code
    <!-- end -->
    something
    that
    does
    not
    interest
    me
    

    如果我使用

    $body=preg_replace("(.*)<!-- start -->(.*)<!-- end -->(.*)","$2",$body);
    

    我得到:

    Warning: preg_replace() [function.preg-replace]: Unknown modifier '<' 
    

    我该怎么纠正?

    2 回复  |  直到 16 年前
        1
  •  17
  •   Matteo Riva    16 年前

    preg 模式需要一对字符来限定模式本身。在这里,你的模式被包含在第一对括号里,其他的都在外面。

    试试这个:

    $body=preg_replace("/(.*)<!-- start -->(.*)<!-- end -->(.*)/","$2",$body);
    

    这只是语法问题,不能保证模式本身看起来可疑。

    编辑:

    假设示例中的文本:

    preg_match('#<!-- start -->(.*?)<!-- end -->#s', $text, $match);
    $inner_text = trim($match[1]);
    
        2
  •  3
  •   Sarfraz    16 年前

    试试这个:

    $body = preg_replace("/(.*)<!-- start -->(.*)<!-- end -->(.*)/","$2",$body);
    
    推荐文章