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
  • 451,927
  • 24
  • 239
  • 318
user2217947
  • 37
  • 1
  • 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);
Bas van Stein
  • 10,298
  • 3
  • 25
  • 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
  • 451,927
  • 24
  • 239
  • 318
0

add \ in your pattern:

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

and here reference.

Community
  • 1
  • 1
Gouda Elalfy
  • 6,558
  • 1
  • 24
  • 37
0

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

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