3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Html { public static function load($html) { $document = <<<EOD <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> <body>!html</body> </html> EOD; // PHP's \DOMDocument serialization adds extra whitespace when the markup // of the wrapping document contains newlines, so ensure we remove all // newlines before injecting the actual HTML body to be processed. $document = strtr($document, ["\n" => '', '!html' => $html]); $dom = new \DOMDocument(); // Ignore warnings during HTML soup loading. @$dom->loadHTML($document); return $dom; } /** * Converts the body of a \DOMDocument back to an HTML snippet. * * The function serializes the body part of a \DOMDocument back to an (X)HTML * snippet. The resulting (X)HTML snippet will be properly formatted to be * compatible with HTML user agents. * * @param \DOMDocument $document * A \DOMDocument object to serialize, only the tags below the first <body> * node will be converted. * * @return string * A valid (X)HTML snippet, as a string. */ public static function serialize(\DOMDocument $document) { $body_node = $document->getElementsByTagName('body')->item(0); $html = ''; if ($body_node !== NULL) { foreach ($body_node->childNodes as $node) { $html .= $document->saveXML($node); } } return $html; } } $source = <<<EOT <table> <tr> <td>L</td> <td>R</td> </tr> </table> EOT; $document = Html::load($source); $out['document'] = "\n" . $document->saveHTML(); $body = $document->getElementsByTagName('body')->item(0); $out['body'] = "\n" . $document->saveXML($body); $table = $body->childNodes[0]; $out['table'] = "\n" . $document->saveXML($table); $out['serialize'] = "\n" . Html::serialize($document); var_export($out);
Output for 8.0.30, 8.1.22 - 8.1.28, 8.2.9 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array ( 'document' => ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><table> <tr> <td>L</td> <td>R</td> </tr> </table></body></html> ', 'body' => ' <body><table> <tr> <td>L</td> <td>R</td> </tr> </table></body>', 'table' => ' <table> <tr> <td>L</td> <td>R</td> </tr> </table>', 'serialize' => ' <table> <tr> <td>L</td> <td>R</td> </tr> </table>', )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array ( 'document' => ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><table> <tr> <td>L</td> <td>R</td> </tr> </table></body></html> ', 'body' => ' <body><table> <tr> <td>L</td> <td>R</td> </tr> </table></body>', 'table' => ' <table> <tr> <td>L</td> <td>R</td> </tr> </table>', 'serialize' => ' <table> <tr> <td>L</td> <td>R</td> </tr> </table>', )
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.29, 8.1.0 - 8.1.21, 8.2.0 - 8.2.8
array ( 'document' => ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><table><tr><td>L</td> <td>R</td> </tr></table></body></html> ', 'body' => ' <body><table><tr><td>L</td> <td>R</td> </tr></table></body>', 'table' => ' <table><tr><td>L</td> <td>R</td> </tr></table>', 'serialize' => ' <table><tr><td>L</td> <td>R</td> </tr></table>', )

preferences:
136.51 ms | 403 KiB | 153 Q