简体   繁体   中英

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);

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>');

@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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM