0

I keep getting this error when using preg_replace that's mixed with some html.

Warning: preg_replace(): Unknown modifier 'd'

here is the code I used.

it's removing that bit of html from the beginning of a string that contains html.

$foo = preg_replace("/^<div id='IDHERE'>sometext.</div>/", '', $foo);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
user2217947
  • 37
  • 2
  • 6

4 Answers4

1

You need to escape the / because it is used to end the first part of the regex string, after which a modifier such as g (global) is used and d is not a valid option there (the d from div).

Make it \/

            $foo = preg_replace("/^<div id='IDHERE'>sometext.<\/div>/", '', $foo);
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
0

Escape the closing </div> tag:

$foo = preg_replace("/^<div id='IDHERE'>sometext.<\/div>/", '', $foo);

PHP was interpreting the forward slash in the closing div tag as being the end of the expression:

/^<div id='IDHERE'>sometext.</div>/
                             ^ PHP thinks this is the end,
                               then complains about the unknown modifier 'd'
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

add \ in your pattern:

 $foo = preg_replace("/^<div id='IDHERE'>sometext.<\/div>/", '', $foo);

and here reference.

Community
  • 1
  • 1
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
0

You need to escape / in </div>, so your regex should be:

$foo = preg_replace("/^<div id='IDHERE'>sometext.<\/div>/", '', $foo);
Jeffwa
  • 1,143
  • 10
  • 12