假设$ body等于
something
that
does
not
interest
me
<!-- start -->
some
html
code
<!-- end -->
something
that
does
not
interest
me
Run Code Online (Sandbox Code Playgroud)
如果我使用
$body=preg_replace("(.*)<!-- start -->(.*)<!-- end -->(.*)","$2",$body);
Run Code Online (Sandbox Code Playgroud)
我获得:
something
that
does
not
interest
me
<!-- start -->
some
html
code
<!-- end -->
something
that
does
not
interest
me
Run Code Online (Sandbox Code Playgroud)
我该如何纠正?
Mat*_*iva 16
甲preg
图案需要一对限定所述图案本身的字符.在这里,您的模式包含在第一对括号中,其他所有内容都在外面.
试试这个:
$body=preg_replace("/(.*)<!-- start -->(.*)<!-- end -->(.*)/","$2",$body);
Run Code Online (Sandbox Code Playgroud)
这只是语法,不能保证模式本身看起来很可疑.
编辑:
假设您的示例中的文本:
preg_match('#<!-- start -->(.*?)<!-- end -->#s', $text, $match);
$inner_text = trim($match[1]);
Run Code Online (Sandbox Code Playgroud)