2012-04-11 36 views
3

在我的代碼,我有上一層> saveHTML()方法轉換 空間

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

我處理包含 的HTML:

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

和結果:

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

如何防止&nbsp;轉換爲空白?

+0

你在做什麼樣的xpath和處理,實體被刪除?你使用正常化的空間還是類似的東西? – hakre 2012-05-30 13:36:54

+1

可能相關:[PHP的DOMNode實體和nodeValue](http://stackoverflow.com/questions/2752434/php-domnode-entities-and-nodevalue) – hakre 2012-06-12 11:31:28

+0

謝謝,我會嘗試這個選項 – ltfishie 2012-06-12 17:44:12

回答

0

替換& nbsp;與& amp; nbsp; 然後當閱讀htmlDom文檔時,它會返回& nbsp;

+0

謝謝我會試試這個。 – ltfishie 2012-04-12 00:47:27

+0

其結果是& nbsp;留在頁面上。 – ltfishie 2012-04-12 02:07:05

+1

雖然好iidea。我最終做了兩個替換,使其工作。用@nbsp;替換 在開始時,並替換@nbsp;最後用 。 – ltfishie 2012-04-12 02:20:18

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