PhpDOMDocument recursively formats indent HTML documents ,. PhpDOMDocument recursively formats indent HTML documents, functionformat (DOMNode $ node, $ treeIndex0) {unformatted tag if (in_array ($ node-nodeName, array (title, p php DOMDocument recursively formats indent HTML documents,
Function format (\ DOMNode $ node, $ treeIndex = 0) {// unformatted label 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 ='
'; $ Doc = new \ DOMDocument (); // $ doc-> formatOutput = true; // I don't know if it is my understanding, this option is incorrectly formatted. $ doc-> loadHTML ($ html); format ($ doc-> documentElement); echo $ doc-> saveHTML ();
Http://www.bkjia.com/PHPjc/1102843.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1102843.htmlTechArticlephp DOMDocument recursive formatting indent HTML document, function format (\ DOMNode $ node, $ treeIndex = 0) {// unformatted tag if (in_array ($ node-nodeName, array ("title", "p "...