PHP DOMDocument用div ID替换内容 [英] PHP DOMDocument replace content by div id
本文介绍了PHP DOMDocument用div ID替换内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试重新分配div的内容,就像在jQuery .html()中一样.这是我到目前为止所做的:
I am trying to repalce content of div like in jQuery .html(). This is what I have done so far:
$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();
任何帮助将不胜感激!谢谢.
Any help would be appreciated! Thank you.
推荐答案
您可以使用以下代码,只需更改 nodeValue
You can use the following code, you just need to change the nodeValue
$doc->loadHTML($html);
$doc->documentGetElementById('text')->nodeValue = "<p>This is some new text</p>";
echo $doc->saveHTML();
这篇关于PHP DOMDocument用div ID替换内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文