问题
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.
回答1:
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();
来源:https://stackoverflow.com/questions/18283721/php-domdocument-replace-content-by-div-id