创建 innerHTML 和 outerHTML
<?php
类 DOMHTMLElement 扩展 DOMElement
{
函数 __construct() { parent::__construct();}
public 函数 innerHTML()
{
$doc = new DOMDocument();
遍历 ($this->childNodes 作为 $child){
$doc->appendChild($doc->importNode($child, true));
}
$content = $doc->saveHTML();
返回 $content;
}
public 函数 outerHTML()
{
$doc = new DOMDocument();
$doc->appendChild($doc->importNode($this, true));
$content = $doc->saveHTML();
返回 $content;
}
}
$dom = DOMDocument::loadHTMLFile($file);
$dom->registerNodeClass('DOMElement','DOMHTMLElement');
如果($dom)
{
$xpath = new DOMXpath($dom);
$regions = $xpath->query("//*[contains(@class, 'editable')]");
$content = '';
遍历($regions 作为 $region){
$content .= $region->outerHTML();
}
返回 $content;
}其他{
抛出 new Exception('无法解析 HTML。请验证语法是否正确。');
}
?>