2010-03-26 88 views
6

让我们假设$身体等于的preg_replace:未知的修饰

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 '<' 

如何有我纠正?

+3

它可能是更容易得到的* *开始的位置和结束* *在'strpos'的位置,然后让只是这些位置与'substr'之间的部分。 – Gumbo 2010-03-26 17:48:05

回答

17

preg图案需要一对限定所述图案本身的字符。这里你的模式被包含在第一对括号中,而其他所有内容都在外面。

试试这个:

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

这只是有关语法上看起来可疑图案自身难保。

编辑:

假设在你的榜样文字:

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

用“$ 2”代替“”解决了这个问题。仍然有一个问题,使用2美元会影响其他任何区域吗? – Vinith 2014-02-11 13:58:59

3

尝试这种情况:

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