Preg replace etting error

39 Views Asked by At

I am getting error wit preg replace.

Here is my code

$search='+5DR';
$replace='5';
$tempString='1.2 EASY +5DR';
echo $tempString = preg_replace('~\b('. $search.')\b~', $replace, $tempString);

Error Details

<br />
<b>Warning</b>:  preg_replace(): 
Compilation failed: nothing to repeat at 
offset 3 in <b>[...][...]</b> on line <b>6</b><br />
1

There are 1 best solutions below

0
On

+ is a reserved preg operator, as such it should be properly escaped if it is part of the mask.

$search=preg_quote('+5DR');

See http://php.net/manual/en/function.preg-quote.php