title: Remove HTML Attributes using PHP
body: <p>Using <strong>PHP</strong> i want to remove all <strong>HTML</strong> attributes except </p>
  
  <blockquote>
    <p><strong>"src"</strong> attribute from <strong>"img"</strong> tag</p>
  </blockquote>
  
  <p>and </p>
  
  <blockquote>
    <p><strong>"href"</strong> attribute from <strong>"a"</strong> tag</p>
  </blockquote>
  
  <p>My Input file is <strong>.html</strong> file which is been converted from <strong>.doc</strong> and <strong>.docx</strong></p>
  
  <p>My output file again should be <strong>HTML file</strong> with removed attribute </p>
  
  <p>Kindly help me please</p>
  
  <p><strong><em>Edit ::</em></strong></p>
  
  <p>After Trying alexander script as below if i open the strip.html in code editor i don't see any changes</p>
  
  <pre><code>&lt;?php
  $path = '/var/www/strip.html';
  $html = file_get_contents($path);
  $dom = new DOMDocument();
  $dom-&gt;strictErrorChecking = false;
  $dom-&gt;formatOutput = true;
  $dom-&gt;loadHTML($html);
  
  $xpath = new DOMXPath($dom);
  if (false === ($elements = $xpath-&gt;query("//img"))) die('Error');
  
  foreach ($elements as $element) {
      for ($i = $element-&gt;attributes-&gt;length; --$i &gt;= 0;) {
          $name = $element-&gt;attributes-&gt;item($i)-&gt;name;
          if ('src' !== $name) {
              $element-&gt;removeAttribute($name);
          }
      }
  }
  
  if (false === ($elements = $xpath-&gt;query("//a"))) die('Error');
  
  foreach ($elements as $element) {
      for ($i = $element-&gt;attributes-&gt;length; --$i &gt;= 0;) {
          $name = $element-&gt;attributes-&gt;item($i)-&gt;name;
          if ('href' !== $name) {
              $element-&gt;removeAttribute($name);
          }
      }
  }
  
  $dom-&gt;saveHTMLFile($path);
  
  ?&gt;
  </code></pre>