Preg_replace: unknown modifier

Assume $ body is equal

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

If i use

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

I get:

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

How do i fix it?

+5
php preg-replace
source share
2 answers

A preg pattern needs a pair of characters that limit the pattern itself. Here, your template is enclosed in the first pair of brackets, and everything else is outside.

Try this:

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

It is only a matter of syntax, and there is no guarantee on the template itself, which looks suspicious.

Suppose the text in your example:

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

Try the following:

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

All Articles