Reado
Reado

Reputation: 1452

PHP preg_replace to replace content within specific tags

I want to change this:

<div>
  <div>
    <div id="myList">
      <table><tr><td></td></tr></table>
    </div>
  </div>
</div>

Into this:

<table><tr><td></td></tr></table>

What would the preg_replace() be? I have tried the following but to no avail:

$result = preg_replace('#<div(.*?)(?! id="myList")>(.*?)</div>#is', '', $result);

Upvotes: 1

Views: 493

Answers (2)

mhmd
mhmd

Reputation: 254

@Reado try this

$s='<div>
  <div>
    <div id="myList">
      <table><tr><td></td></tr></table>
    </div>
  </div>
</div>';
    $replace=str_replace(array('<div>', '<div id="myList">','</div>'),"",$s);

Upvotes: 1

Mihai Matei
Mihai Matei

Reputation: 24276

You can use strip_tags

$string = '<div>
  <div>
    <div id="myList">
      <table><tr><td></td></tr></table>
    </div>
  </div>
</div>';

echo strip_tags($string, '<table><tr><td>');

Upvotes: 4

Related Questions