3v4l.org

run code in 300+ PHP versions simultaneously
<?php 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 EOD; // PHP's \DOMDocument::saveXML() encodes carriage returns as &#13; so // normalize all newlines to line feeds. $html = str_replace(["\r\n", "\r"], "\n", $html); // 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, LIBXML_NOBLANKS); return $dom; } 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; } print _serialize(load('<a class="sample" href="http://www.example.com/partial/path">foo</a>')); print PHP_EOL; print _serialize(load('<a class="sample" href="http://www.example.com/partial/path'));
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
<a class="sample" href="http://www.example.com/partial/path">foo</a> <a class="sample" href="http://www.example.com/partial/path"></a>

preferences:
121.66 ms | 402 KiB | 89 Q