PHP Parsing HTML Parsing HTML from a string


PHP implements a DOM Level 2 compliant parser, allowing you to work with HTML using familiar methods like getElementById() or appendChild().

$html = '<html><body><span id="text">Hello, World!</span></body></html>';

$doc = new DOMDocument();

echo $doc->getElementById("text")->textContent;


Hello, World!

Note that PHP will emit warnings about any problems with the HTML, especially if you are importing a document fragment. To avoid these warnings, tell the DOM library (libxml) to handle its own errors by calling libxml_use_internal_errors() before importing your HTML. You can then use libxml_get_errors() to handle errors if needed.