public static Html::load($html)
Parses an HTML snippet and returns it as a DOM object.
This function loads the body part of a partial (X)HTML document and returns a full \DOMDocument object that represents this document.
Use \Drupal\Component\Utility\Html::serialize() to serialize this \DOMDocument back to a string.
string $html: The partial (X)HTML snippet to load. Invalid markup will be corrected on import.
\DOMDocument A \DOMDocument that represents the loaded (X)HTML snippet.
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, array("\n" => '', '!html' => $html)); $dom = new \DOMDocument(); // Ignore warnings during HTML soup loading. @$dom->loadHTML($document); return $dom; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!Utility!Html.php/function/Html::load/8.1.x