In my code, I have
$document = DomDocument->loadHTML($someHTML);
$xPath = new DOMXPath($document);
//
//do some xpath query and processing
//
$result = $d
replace with   then when the htmlDom doc is read it will return
$someHTML = str_replace (' ', '@nbsp;', $someHTML);
$document = DomDocument->loadHTML($someHTML);
$xPath = new DOMXPath($document);
//
//do some xpath query and processing
//
$result = $document->saveHTML();
$result = str_replace ('@nbsp;', ' ', $result);