2013-08-16 28 views
2

我想要像在jQuery .html()中一樣重新生成div的內容。這是我迄今爲止所做的:PHP DOMDocument取代div的內容編號

$html = " 
<html> 
    <body> 
     <div id="main"> 
      <div id="text"> 
       <p>This is some text</p> 
      </div> 
     </div> 
    </body> 
</html> 
"; 

$doc = new DOMDocument(); 
$doc->loadHTML($html); 
// change div id "text" contents 
// $("#text").html("<p>This is some new text</p>"); 
echo $doc->saveHTML(); 

任何幫助,將不勝感激!謝謝。

回答

4

您可以使用下面的代碼,你只需要改變nodeValue

$doc->loadHTML($html); 
$doc->documentGetElementById('text')->nodeValue = "<p>This is some new text</p>"; 
echo $doc->saveHTML(); 
+0

謝謝!像魅力一樣工作。 – user2086252

+5

是不是getElementById而不是documentGetElementById? – Chrisissorry