2012-04-11 4 views
3

In meinem Code, ich habeDomDocument-> saveHTML() Umwandlung   Platz

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

Die html Ich bin Verarbeitung enthält  :

<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> 

und Ergebnisse 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> 

Wie verhindere ich, dass &nbsp; in Leerzeichen konvertiert wird?

+0

Welche Art von Xpath und Verarbeitung machst du, dass die Entität entfernt wird? Verwenden Sie Normalize Space oder etwas ähnliches? – hakre

+1

Wahrscheinlich verwandt: [PHP DOMNode entities und nodeValue] (http://stackoverflow.com/questions/2752434/php-domnode-entities-and-nodevalue) – hakre

+0

Danke, ich werde diese Option ausprobieren – ltfishie

Antwort

0

ersetzen & nbsp; mit & amp; nbsp; dann, wenn das htmlDom doc gelesen wird, wird & nbsp zurückgegeben;

+0

Danke werde ich das ausprobieren . – ltfishie

+0

Das Ergebnis ist, dass & nbsp; ist auf der Seite übrig. – ltfishie

+1

Gute iidea obwohl. Ich mache am Ende zwei ersetzen, damit es funktioniert. Ersetzen Sie   durch @nbsp; am Anfang und ersetzen @ nbsp; mit   am Ende. – ltfishie

3
$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);