我正在尝试替换jQuery .html()中的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();
任何帮助将不胜感激!谢谢。
答案 0 :(得分:3)
您可以使用以下代码,只需更改nodeValue
$doc->loadHTML($html);
$doc->documentGetElementById('text')->nodeValue = "<p>This is some new text</p>";
echo $doc->saveHTML();