title: DOMDocument remove script tags from HTML source
body: <p>I used <a href="https://stackoverflow.com/questions/7130867/remove-script-tag-from-html-content">@Alex's approach here</a> to remove script tags from a HTML document using the built in DOMDocument. The problem is if I have a script tag with Javascript content and then another script tag that links to an external Javascript source file, not all script tags are removed from the HTML.</p>
  
  <pre><code>$result = '
  &lt;!doctype html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;title&gt;
              hey
          &lt;/title&gt;
          &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt;
          &lt;script&gt;
              alert("hello");
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;hey&lt;/body&gt;
  &lt;/html&gt;
  ';
  
  $dom = new DOMDocument();
  if($dom-&gt;loadHTML($result))
  {
      $script_tags = $dom-&gt;getElementsByTagName('script');
  
      $length = $script_tags-&gt;length;
  
      for ($i = 0; $i &lt; $length; $i++) {
          if(is_object($script_tags-&gt;item($i)-&gt;parentNode)) {
              $script_tags-&gt;item($i)-&gt;parentNode-&gt;removeChild($script_tags-&gt;item($i));
          }
      }
  
      echo $dom-&gt;saveHTML();
  }
  </code></pre>
  
  <p>The above code outputs:</p>
  
  <p></p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;title&gt;hey&lt;/title&gt;
          &lt;script&gt;
          alert("hello");
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          hey
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>As you can see from the output, only the external script tag was removed. Is there anything I can do to ensure all script tags are removed?</p>