Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_replace: unknown modifier [duplicate]

Let's assume that $body is equal to

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 obtain:

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

How have I to correct?

like image 684
tic Avatar asked Mar 26 '10 17:03

tic


2 Answers

A preg pattern needs a pair of characters which delimit the pattern itself. Here your pattern is enclosed in the first pair of parentheses and everything else is outside.

Try this:

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

This is just about the syntax, and there is no guarantee on the pattern itself which looks suspicious.

Assuming the text in your example:

preg_match('#<!-- start -->(.*?)<!-- end -->#s', $text, $match);
$inner_text = trim($match[1]);
like image 167
Matteo Riva Avatar answered Oct 16 '22 00:10

Matteo Riva


Try this:

$body = preg_replace("/(.*)<!-- start -->(.*)<!-- end -->(.*)/","$2",$body);
like image 21
Sarfraz Avatar answered Oct 16 '22 02:10

Sarfraz