爱程序网

php DOMDocument 递归 格式化缩进HTML文档

来源: 阅读:

function format(DOMNode $node, $treeIndex = 0){    //不格式化的标签    if (in_array($node->nodeName, array("title", "p", "span")))        return;    if ($node->hasChildNodes()) {        $treeIndex++;        $tabStart = "rn" . str_repeat("TTT", $treeIndex);        $tabEnd = "rn" . str_repeat("EEE", $treeIndex - 1);        $i = 0;        while ($childNode = $node->childNodes->item($i++)) {            if ($childNode->nodeType == XML_TEXT_NODE) {                if (ctype_space($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(); 

 

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助