php DOMDocument 遞歸 格式化縮進HTML文檔,
function format(\DOMNode $node, $treeIndex = 0)
{
//不格式化的標簽
if (in_array($node->nodeName, array("title", "p", "span")))
return;
if ($node->hasChildNodes()) {
$treeIndex++;
$tabStart = "\r\n" . str_repeat("TTT", $treeIndex);
$tabEnd = "\r\n" . str_repeat("EEE", $treeIndex - 1);
$i = 0;
while ($childNode = $node->childNodes->item($i++)) {
if ($childNode->nodeType == XML_TEXT_NODE) {
if (preg_match('#^\s*$#', $childNode->nodeValue)) {
$node->removeChild($childNode);
$i--;
continue;
}
$childNode->nodeValue = trim($childNode->nodeValue);
}
$node->insertBefore($node->ownerDocument->createTextNode($tabStart), $childNode);
$i++;
$this->format($childNode, $treeIndex);
};
$node->appendChild($node->ownerDocument->createTextNode($tabEnd));
}
}
$html = '<!DOCTYPE html><html><head><meta charset="utf-8"><title></title></head><body></body></html>';
$doc = new \DOMDocument();
//$doc->formatOutput = true; //不知道是不是我的理解問題,這個選項格式化出來的並不完美
$doc->loadHTML($html); format($doc->documentElement);
echo $doc->saveHTML();