I'm trying to find paragraphs with the id "test" and remove them from a html string, I've tried using php DOMDocument but the html I'm searching is badly formed and I get errors
$caption = "blah blah<p id ='test'>Test message</p>";
$doc = new DOMDocument();
$doc->loadHTMLFile($caption);
$xmessage = $doc->getElementById('test');
returns
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Unexpected end tag : br i
Is there a way to suppress the warnings? Thanks
You can use following code to remove a para with id='test'
:
$caption = "blah blah<p id='test'>Test message</p><p id='foo'>Foo Bar</p>";
$doc = new DOMDocument();
$doc->loadHTML($caption);
$xpath = new DOMXPath($doc);
$nlist = $xpath->query("//p[@id='test']");
$node = $nlist->item(0);
echo "Para: [" . $node->nodeValue . "]\n";
$node->parentNode->removeChild($node);
echo "Remaining: [" . $doc->saveHTML() . "]\n";
OUTPUT:
Para: [Test message]
Remaining: [<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<p>blah blah</p>
<p id="foo">Foo Bar</p>
</body></html>
]