PHP DOMDocument Recursive formatting of indented HTML documents
functionFormat (\domnode$node,$treeIndex= 0){ //Non-formatted labelif(In_array($node->nodename,Array("title", "P", "span"))) return; if($node-HasChildNodes ()) { $treeIndex++; $tabStart= "\ r \ n".str_repeat(" ",$treeIndex); $tabEnd= "\ r \ n".str_repeat(" ",$treeIndex-1); $i= 0; while($childNode=$node->childnodes->item ($i++)) { //Remove the empty text nodeif($childNode->nodetype = = Xml_text_node andPreg_match(' #^\s*$# ',$childNode-nodevalue)) { $node->removechild ($childNode); $i--; Continue; } $node->insertbefore ($node->ownerdocument->createtextnode ($tabStart),$childNode); $i++; Format ($childNode,$treeIndex); }; $node->appendchild ($node->ownerdocument->createtextnode ($tabEnd)); }}$html= '
<title></title>';$doc=New\domdocument ();//$doc->formatoutput = true;//I don't know if it is my understanding that this option is not a perfect format.$doc->loadhtml ($html); Format ($doc-documentelement);Echo$doc->savehtml ();
The above describes the PHP DOMDocument recursive formatting indented HTML documents, including the content of the text, I hope that the PHP tutorial interested in a friend to help.