<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Jonhoo's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 29 Aug 2008 20:17:38 GMT</pubDate>
    <description>DZone Snippets: Jonhoo's Code Snippets</description>
    <item>
      <title>PHP function for cleaning up HTML and JavaSctipt code</title>
      <link>http://snippets.dzone.com/posts/show/1964</link>
      <description>// This is a function for PHP scripts to clean up HTML code before outputting it.&lt;br /&gt;// The function applies correct indentation to HTML/XHTML 1.0 and JavaScript&lt;br /&gt;// And makes the output much more readable.&lt;br /&gt;// You can specify the wanted indentation through the variable $indent&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;//Function to seperate multiple tags one line&lt;br /&gt;function fix_newlines_for_clean_html($fixthistext)&lt;br /&gt;{&lt;br /&gt;	$fixthistext_array = explode("\n", $fixthistext);&lt;br /&gt;	foreach ($fixthistext_array as $unfixedtextkey =&gt; $unfixedtextvalue)&lt;br /&gt;	{&lt;br /&gt;		//Makes sure empty lines are ignores&lt;br /&gt;		if (!preg_match("/^(\s)*$/", $unfixedtextvalue))&lt;br /&gt;		{&lt;br /&gt;			$fixedtextvalue = preg_replace("/&gt;(\s|\t)*&lt;/U", "&gt;\n&lt;", $unfixedtextvalue);&lt;br /&gt;			$fixedtext_array[$unfixedtextkey] = $fixedtextvalue;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	return implode("\n", $fixedtext_array);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function clean_html_code($uncleanhtml)&lt;br /&gt;{&lt;br /&gt;	//Set wanted indentation&lt;br /&gt;	$indent = "    ";&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	//Uses previous function to seperate tags&lt;br /&gt;	$fixed_uncleanhtml = fix_newlines_for_clean_html($uncleanhtml);&lt;br /&gt;	$uncleanhtml_array = explode("\n", $fixed_uncleanhtml);&lt;br /&gt;	//Sets no indentation&lt;br /&gt;	$indentlevel = 0;&lt;br /&gt;	foreach ($uncleanhtml_array as $uncleanhtml_key =&gt; $currentuncleanhtml)&lt;br /&gt;	{&lt;br /&gt;		//Removes all indentation&lt;br /&gt;		$currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml);&lt;br /&gt;		$currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml);&lt;br /&gt;		&lt;br /&gt;		$replaceindent = "";&lt;br /&gt;		&lt;br /&gt;		//Sets the indentation from current indentlevel&lt;br /&gt;		for ($o = 0; $o &lt; $indentlevel; $o++)&lt;br /&gt;		{&lt;br /&gt;			$replaceindent .= $indent;&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		//If self-closing tag, simply apply indent&lt;br /&gt;		if (preg_match("/&lt;(.+)\/&gt;/", $currentuncleanhtml))&lt;br /&gt;		{ &lt;br /&gt;			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;&lt;br /&gt;		}&lt;br /&gt;		//If doctype declaration, simply apply indent&lt;br /&gt;		else if (preg_match("/&lt;!(.*)&gt;/", $currentuncleanhtml))&lt;br /&gt;		{ &lt;br /&gt;			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;&lt;br /&gt;		}&lt;br /&gt;		//If opening AND closing tag on same line, simply apply indent&lt;br /&gt;		else if (preg_match("/&lt;[^\/](.*)&gt;/", $currentuncleanhtml) &amp;&amp; preg_match("/&lt;\/(.*)&gt;/", $currentuncleanhtml))&lt;br /&gt;		{ &lt;br /&gt;			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;&lt;br /&gt;		}&lt;br /&gt;		//If closing HTML tag or closing JavaScript clams, decrease indentation and then apply the new level&lt;br /&gt;		else if (preg_match("/&lt;\/(.*)&gt;/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml))&lt;br /&gt;		{&lt;br /&gt;			$indentlevel--;&lt;br /&gt;			$replaceindent = "";&lt;br /&gt;			for ($o = 0; $o &lt; $indentlevel; $o++)&lt;br /&gt;			{&lt;br /&gt;				$replaceindent .= $indent;&lt;br /&gt;			}&lt;br /&gt;			&lt;br /&gt;			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;&lt;br /&gt;		}&lt;br /&gt;		//If opening HTML tag AND not a stand-alone tag, or opening JavaScript clams, increase indentation and then apply new level&lt;br /&gt;		else if ((preg_match("/&lt;[^\/](.*)&gt;/", $currentuncleanhtml) &amp;&amp; !preg_match("/&lt;(link|meta|base|br|img|hr)(.*)&gt;/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml))&lt;br /&gt;		{&lt;br /&gt;			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;&lt;br /&gt;			&lt;br /&gt;			$indentlevel++;&lt;br /&gt;			$replaceindent = "";&lt;br /&gt;			for ($o = 0; $o &lt; $indentlevel; $o++)&lt;br /&gt;			{&lt;br /&gt;				$replaceindent .= $indent;&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		else&lt;br /&gt;		//Else, only apply indentation&lt;br /&gt;		{$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;}&lt;br /&gt;	}&lt;br /&gt;	//Return single string seperated by newline&lt;br /&gt;	return implode("\n", $cleanhtml_array);	&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 27 Apr 2006 13:53:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1964</guid>
      <author>Jonhoo ()</author>
    </item>
  </channel>
</rss>
