Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Do not use RegEx on HTML. PHP provides a tool for parsing DOM structures, called appropriately DomDocument.</p> <pre><code>&lt;?php // some HTML for example $myHtml = '&lt;html&gt;&lt;head&gt;&lt;script&gt;alert("hi mom!");&lt;/script&gt;&lt;/head&gt;&lt;body&gt;&lt;style&gt;body { color: red;} &lt;/style&gt;&lt;h1&gt;This is some content&lt;/h1&gt;&lt;p&gt;content is awesome&lt;/p&gt;&lt;/body&gt;&lt;script src="someFile.js"&gt;&lt;/script&gt;&lt;/html&gt;'; // create a new DomDocument object $doc = new DOMDocument(); // load the HTML into the DomDocument object (this would be your source HTML) $doc-&gt;loadHTML($myHtml); removeElementsByTagName('script', $doc); removeElementsByTagName('style', $doc); removeElementsByTagName('link', $doc); // output cleaned html echo $doc-&gt;saveHtml(); function removeElementsByTagName($tagName, $document) { $nodeList = $document-&gt;getElementsByTagName($tagName); for ($nodeIdx = $nodeList-&gt;length; --$nodeIdx &gt;= 0; ) { $node = $nodeList-&gt;item($nodeIdx); $node-&gt;parentNode-&gt;removeChild($node); } } </code></pre> <p>You can try it here: <a href="https://eval.in/private/4f225fa0dcb4eb">https://eval.in/private/4f225fa0dcb4eb</a></p> <p><strong>Documentation</strong></p> <ul> <li><code>DomDocument</code> - <a href="http://php.net/manual/en/class.domdocument.php">http://php.net/manual/en/class.domdocument.php</a></li> <li><code>DomNodeList</code> - <a href="http://php.net/manual/en/class.domnodelist.php">http://php.net/manual/en/class.domnodelist.php</a></li> <li><code>DomDocument::getElementsByTagName</code> - <a href="http://us3.php.net/manual/en/domdocument.getelementsbytagname.php">http://us3.php.net/manual/en/domdocument.getelementsbytagname.php</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload