public static function new_from_string(h\string $template) { $document = new \domdocument('1.1', 'UTF-8'); $document->loadHTML($template); return new static($document); }
private static function tidyUpModuleDom($string) { $dom = new domdocument(); @$dom->loadHTML("<html><body>" . $string . "</body></html>"); $string = ''; foreach ($dom->documentElement->firstChild->childNodes as $child) { $string .= $dom->saveXML($child); } return $string; }
<?php $dom = new domdocument(); $html = <<<EOM <html><head> <title>Hello world</title> </head> <body> This is a not well-formed<br> html files with undeclared entities </body> </html> EOM; $dom->loadHTML($html); print "--- save as XML\n"; print adjustDoctype($dom->saveXML()); print "--- save as HTML\n"; print adjustDoctype($dom->saveHTML()); function adjustDoctype($xml) { return str_replace(array(">\n<", "DOCTYPE HTML", '<p>', '</p>'), array("><", "DOCTYPE html", '', ''), $xml); }