简体   繁体   English

DOMElement::innerHTML 的原始内容

[英]Raw content of DOMElement::innerHTML

How, in PHP, I can get the raw content of DOMElement , like JS innerHTML does?在 PHP 中,我如何获取DOMElement的原始内容,就像 JS innerHTML那样?

I tried with saveHTML() or saveXML() iterating over each childNodes to simulate innerHTML , but it replaced code like turning <br /> to <br> or <br/> (in case of the XML version).我尝试使用saveHTML()saveXML()迭代每个childNodes来模拟innerHTML ,但它替换了代码,例如将<br />变成<br><br/> (在 XML 版本的情况下)。

This can be achieved in a hacky but reliable way.这可以通过一种笨拙但可靠的方式来实现。 PHP has the equivalent of outerHTML by passing the node to its parent document's saveHTML() method.通过将节点传递给其父文档的saveHTML()方法,PHP 具有与outerHTML等效的功能。 Because this output is well-formed and escaped, you can easily strip the single outer tag from the text, leaving the desired innerHTTML .由于此输出格式良好且已转义,因此您可以轻松地从文本中innerHTTML单个外部标记,留下所需的innerHTTML

Example:示例:

$dom = new DOMDocument;
$dom->loadHTML('<div><p with="scary<>\'&quot;" attrs=40 ok>Hello <em>World</em></div>');
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//p') as $p) {
    $innerHTML = preg_replace('@^<([^>\\s]+)[^>]*>(.*)</\\1>$@s', '$2', $dom->saveHTML($p));
    var_dump($p);
}

Demo of regex: https://regex101.com/r/yEVMQx/2正则表达式演示: https : //regex101.com/r/yEVMQx/2

Note the s flag on the regex is critical.请注意,正则表达式上的s标志至关重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM