2

I am trying to remove this div

<div id="myid" class="myclass">other_tags_here</div>

I am using

preg_replace ("/<div id=\"myid\" class=\"myclass\">/", "", $buffer)

but this removes only the first opening tag while I want to remove both opening and closing tags as well as all tags in between.

Thank you

Yannis
  • 912
  • 2
  • 14
  • 34
  • This is awfully hard to achieve with `preg_replace()`... – Dennis Dec 05 '11 at 18:45
  • Processing html with regexes will just make [Tony the Pony](http://stackoverflow.com/a/1732454/118068) trot up to your front door and knock out your teeth. – Marc B Dec 05 '11 at 19:36

2 Answers2

5

Don't do this. Please use the very nice PHP DOM facilities.

Although a regex can be written to match your particular case, this will be fragile.

Community
  • 1
  • 1
FailedDev
  • 26,680
  • 9
  • 53
  • 73
1

in order to remove everything in between elements you should do somthing like this

    preg_replace ("/<div id=\"myid\" class=\"myclass\">.*?<\/div>/", "", $buffer);
Xeus
  • 280
  • 1
  • 2
  • 14