PHP function for cleaning up HTML and JavaSctipt code
// The function applies correct indentation to HTML/XHTML 1.0 and JavaScript
// And makes the output much more readable.
// You can specify the wanted indentation through the variable $indent
1 2 <?php 3 4 //Function to seperate multiple tags one line 5 function fix_newlines_for_clean_html($fixthistext) 6 { 7 $fixthistext_array = explode("\n", $fixthistext); 8 foreach ($fixthistext_array as $unfixedtextkey => $unfixedtextvalue) 9 { 10 //Makes sure empty lines are ignores 11 if (!preg_match("/^(\s)*$/", $unfixedtextvalue)) 12 { 13 $fixedtextvalue = preg_replace("/>(\s|\t)*</U", ">\n<", $unfixedtextvalue); 14 $fixedtext_array[$unfixedtextkey] = $fixedtextvalue; 15 } 16 } 17 return implode("\n", $fixedtext_array); 18 } 19 20 function clean_html_code($uncleanhtml) 21 { 22 //Set wanted indentation 23 $indent = " "; 24 25 26 //Uses previous function to seperate tags 27 $fixed_uncleanhtml = fix_newlines_for_clean_html($uncleanhtml); 28 $uncleanhtml_array = explode("\n", $fixed_uncleanhtml); 29 //Sets no indentation 30 $indentlevel = 0; 31 foreach ($uncleanhtml_array as $uncleanhtml_key => $currentuncleanhtml) 32 { 33 //Removes all indentation 34 $currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml); 35 $currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml); 36 37 $replaceindent = ""; 38 39 //Sets the indentation from current indentlevel 40 for ($o = 0; $o < $indentlevel; $o++) 41 { 42 $replaceindent .= $indent; 43 } 44 45 //If self-closing tag, simply apply indent 46 if (preg_match("/<(.+)\/>/", $currentuncleanhtml)) 47 { 48 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml; 49 } 50 //If doctype declaration, simply apply indent 51 else if (preg_match("/<!(.*)>/", $currentuncleanhtml)) 52 { 53 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml; 54 } 55 //If opening AND closing tag on same line, simply apply indent 56 else if (preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && preg_match("/<\/(.*)>/", $currentuncleanhtml)) 57 { 58 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml; 59 } 60 //If closing HTML tag or closing JavaScript clams, decrease indentation and then apply the new level 61 else if (preg_match("/<\/(.*)>/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml)) 62 { 63 $indentlevel--; 64 $replaceindent = ""; 65 for ($o = 0; $o < $indentlevel; $o++) 66 { 67 $replaceindent .= $indent; 68 } 69 70 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml; 71 } 72 //If opening HTML tag AND not a stand-alone tag, or opening JavaScript clams, increase indentation and then apply new level 73 else if ((preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && !preg_match("/<(link|meta|base|br|img|hr)(.*)>/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml)) 74 { 75 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml; 76 77 $indentlevel++; 78 $replaceindent = ""; 79 for ($o = 0; $o < $indentlevel; $o++) 80 { 81 $replaceindent .= $indent; 82 } 83 } 84 else 85 //Else, only apply indentation 86 {$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;} 87 } 88 //Return single string seperated by newline 89 return implode("\n", $cleanhtml_array); 90 } 91 ?>