How can I get an element's serialised HTML with PHP's DOMDocument?

前端 未结 2 1285
無奈伤痛
無奈伤痛 2020-12-18 10:40

This is my example script:

$html = <<
    
Capture this text 1
&l
2条回答
  •  悲哀的现实
    2020-12-18 11:21

    Well, nodeValue will give you the node's value. You want what's commonly called outerHTML

    echo $dom->saveXml($tag);
    

    will output what you are looking for in an X(HT)ML compliant way.


    As of PHP 5.3.6 you can also pass a node to saveHtml, which wasnt possible previously:

    echo $dom->saveHtml($tag);
    

    The latter will obey HTML4 syntax. Thanks to Artefacto for that.

提交回复
热议问题