创建 innerHTML 和 outerHTML
<?php
class DOMHTMLElement extends DOMElement
{
function __construct() { parent::__construct();}
public function innerHTML()
{
$doc = new DOMDocument();
foreach ($this->childNodes as $child){
$doc->appendChild($doc->importNode($child, true));
}
$content = $doc->saveHTML();
return $content;
}
public function outerHTML()
{
$doc = new DOMDocument();
$doc->appendChild($doc->importNode($this, true));
$content = $doc->saveHTML();
return $content;
}
}
$dom = DOMDocument::loadHTMLFile($file);
$dom->registerNodeClass('DOMElement','DOMHTMLElement');
if($dom)
{
$xpath = new DOMXpath($dom);
$regions = $xpath->query("//*[contains(@class, 'editable')]");
$content = '';
foreach($regions as $region){
$content .= $region->outerHTML();
}
return $content;
}else{
throw new Exception('无法解析 HTML。请验证语法是否正确。');
}
?>