Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMDocument->saveHTML() converting   to space

In my code, I have

$document = DomDocument->loadHTML($someHTML);
$xPath = new DOMXPath($document);
//
//do some xpath query and processing
//
$result = $document->saveHTML();

The html I am processing contains  :

<html>
<body>
<p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal;text-autospace:none"><b><span style='font-size:9.0pt;font-family:"ArialNarrow","sans-serif";
color:red'>&nbsp;</span></b></p>
</body>
</html>

and results in:

<html>
<body>
<p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal;text-autospace:none"><b><span style='font-size:9.0pt;font-family:"ArialNarrow","sans-serif";
color:red'> </span></b></p>
</body>
</html>

How do I prevent &nbsp; from getting converted to blank space?

like image 513
ltfishie Avatar asked Oct 08 '22 03:10

ltfishie


1 Answers

$someHTML = str_replace ('&nbsp;', '@nbsp;', $someHTML);
$document = DomDocument->loadHTML($someHTML);
$xPath = new DOMXPath($document);
//
//do some xpath query and processing
//
$result = $document->saveHTML();
$result = str_replace ('@nbsp;', '&nbsp;', $result);
like image 102
ltfishie Avatar answered Oct 12 '22 12:10

ltfishie