Question: I have a html code:

```
<table id="table1" border="0" cellspacing="0" cellpadding="3" width="1" align="center">
  <tr>
    <td>
    <img src="http://vnexpress.net/Files/Subject/3b/bd/ac/f9/cuongbibat.jpg" width="330" height="441" border="1" alt="C&#432;&#7901;ng">
    </td>
  </tr>
  <tr>
    <td class="Image">Everything
   </td>
  </tr>
</table>
 <table id="table2" border="0" cellspacing="0" cellpadding="3" width="1" align="center">
      <tr>
        <td>
        Someone
        </td>
      </tr>
      <tr>
        <td class="Image">Everything
       </td>
      </tr>
    </table>

```

I have 2 table, i want to remove all tag: table, tr, td if table have img tag(table 1).
I need to get result like :

```
     <img src="http://vnexpress.net/Files/Subject/3b/bd/ac/f9/cuongbibat.jpg" width="330" height="441" border="1" alt="C&#432;&#7901;ng">
        Everything

     <table id="table2" border="0" cellspacing="0" cellpadding="3" width="1" align="center">
          <tr>
            <td>
            Someone
            </td>
          </tr>
          <tr>
            <td class="text">Everything
           </td>
          </tr>
        </table>

```

Please help me. Thank you.

Answer: 
[HTML Purifier](http://htmlpurifier.org/) can be used to strip either all tags or a certain set of tags from a document. It's the go-to solution for basically any HTML tag stripping in PHP - **don't ever use regexes for this** or the sun will burn out and we will all freeze to death in the suffocating darkness.

Try something like:

```
$config->set('HTML.Allowed', 'img');
$purifier = new HTMLPurifier($config);
$output = $filter->purify($YOUR_HTML);

```

You'll need to add a `$config->set('HTML.Allowed', 'TAGNAME');` line for every tag you don't want to get scrubbed away, but it's a price worth paying for the continued lifegiving warmth of the day-star. And also not leaving your site open to XSS attacks and content-eating glitches, I guess.
In theory, it's possible to do this with a single highly complex regexp. It's always easier to do the search-and-replace on separate steps: search for the outer container first, then work on what it contains. 

```
<?php 

header("Content-type: text/plain");

$html = '<table id="table1" border="0" cellspacing="0" cellpadding="3" width="1" align="center">
  <tr>
    <td>
    <img src="http://vnexpress.net/Files/Subject/3b/bd/ac/f9/cuongbibat.jpg" width="330" height="441" border="1" alt="C&#432;&#7901;ng">
    </td>
  </tr>
  <tr>
    <td class="Image">Everything
   </td>
  </tr>
</table>
 <table id="table2" border="0" cellspacing="0" cellpadding="3" width="1" align="center">
      <tr>
        <td>
        Someone
        </td>
      </tr>
      <tr>
        <td class="Image">Everything
       </td>
      </tr>
    </table> ';

$html = preg_replace_callback('/<table\b[^>]*>.*?<\/table>/si', 'removeTableIfImg', $html);

function removeTableIfImg($matches) {
    $table = $matches[0];
    return preg_match('/<img\b[^>]*>/i', $table, $img) 
         ? preg_replace('/<\/?(?:table|td|tr)\b[^>]*>\s*/i', '', $table)
         : $table;
}
echo $html;

?>

```

The first pattern finds the tables. The second pattern (in the callback) checks if there's an image tag. The third removes the table, td, and tr tags.