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 = '<! DOCTYPE html>