PHP DOMDocument用div id替换内容

时间:2013-08-16 23:57:56

标签: php domdocument

我正在尝试替换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();

任何帮助将不胜感激!谢谢。

1 个答案:

答案 0 :(得分:3)

您可以使用以下代码,只需更改nodeValue

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