Php preg_replace:未知修改器

Php preg_replace:未知修改器,php,preg-replace,Php,Preg Replace,假设$body等于 something that does not interest me <!-- start --> some html code <!-- end --> something that does not interest me 什么 那个 做 不 兴趣 我 一些 html 代码 某物 那个 做 不 兴趣 我 如果我使用 $body=preg_replace("(.*)<!-- start -->(.*)<!-- en

假设$body等于

something 
that 
does 
not 
interest 
me 
<!-- start -->
some
html
code
<!-- end -->
something
that
does
not
interest
me
什么 那个 做 不 兴趣 我 一些 html 代码 某物 那个 做 不 兴趣 我 如果我使用

$body=preg_replace("(.*)<!-- start -->(.*)<!-- end -->(.*)","$2",$body);
$body=preg_replace((.*)(.*)(.*)(.*),“$2”,$body);
我获得:

警告:preg_replace()[function.preg replace]:未知修饰符“请尝试以下操作:

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

preg模式需要一对字符来分隔模式本身。在这里,您的模式包含在第一对括号中,其他所有内容都在括号外

试试这个:

$body = preg_replace("/(.*)<!-- start -->(.*)<!-- end -->(.*)/","$2",$body);
$body=preg_replace("/(.*)<!-- start -->(.*)<!-- end -->(.*)/","$2",$body);
$body=preg_replace(“/(.*)(.*)(.*)/”、“$2”、$body);
这只是语法上的问题,对于看起来可疑的模式本身没有任何保证

假设示例中的文本:

preg_match('#<!-- start -->(.*?)<!-- end -->#s', $text, $match);
$inner_text = trim($match[1]);
preg#u match('#(.*)#s',$text,$match);
$inner_text=trim($match[1]);

使用
strpos
获取起始位置和结束位置可能更容易,然后使用
substr
仅获取这些位置之间的部分。可能存在重复(同样的原因,缺少正则表达式分隔符)将“”替换为“$2”解决了问题。我仍然有一个问题,使用2美元是否会影响其他领域?