PHP DOMDocument replace content by div id

别等时光非礼了梦想. 提交于 2021-02-04 19:55:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!