Skip to content

Instantly share code, notes, and snippets.

@stemar
Last active September 23, 2016 03:33
Show Gist options
  • Select an option

  • Save stemar/2c793cb8da632f834fc756a7146c946a to your computer and use it in GitHub Desktop.

Select an option

Save stemar/2c793cb8da632f834fc756a7146c946a to your computer and use it in GitHub Desktop.
DOMDocument::loadHTML() removing invalid tags without adding DOCTYPE and <html> tag
<?php
// Invalid end tag </s>
$html = <<<HTML
<table>
<tr>
<td>123</s></td>
<td>456</td>
</tr>
</table>
HTML;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
libxml_clear_errors();
echo $doc->saveHTML();
@stemar
Copy link
Author

stemar commented Sep 23, 2016

Result: the </s> invalid end tag is removed.

<table>
    <tr>
        <td>123</td>
        <td>456</td>
    </tr>
</table>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment