My code looks like this:

if (!empty($foo))
{
    $doc = new DOMDocument();
    $doc->loadHTML($foo);
    $images = $doc->getElementsByTagName('img');
}

And this is happening:

DOMDocument::loadHTML() [<a href='domdocument.loadhtml'>domdocument.loadhtml</a>]: Document is empty in Entity, line: 2

$foo is an empty string or has invalid markup.

See the second comment (by Shane Harter) here.

Odd because $foo just finished getting passed through HTML Purifier.