How can I evaluate HTML depth in PHP?
<p>I'm writing a PHP framework right now, and have run into somewhat of a formatting problem.</p>

<p>My intention is to load in a combination of a few files, compile them all together, and kick them out as their appropriate HTML. In this process, I intend to indent or outdent as I progress line to line. Right now, I use a combination of simple checks for '&lt;', '&amp;lt/' and '/&amp;gt', ( Couldn't use the real character here ) alongside some XML parsing to achieve this, but I'm sure that there is a better way to do this.</p>

<p>Right now my code gives me something like this:</p>

<pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" /&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
 &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
   &lt;title&gt;Your Sitename - Module - Index&lt;/title&gt;
    &lt;link rel="stylesheet" href="layout/layout/default/css/styles.css" type="text/css" /&gt;
     &lt;link rel="stylesheet" href="php/plugins/MoviePlayer/windows/IE/Silverlight/css/silverlight.css" type="text/css" /&gt;
      &lt;script type="text/javascript" src="js/Cookies.js"&gt;&lt;/script&gt;
       &lt;script type="text/javascript" src="js/GlobalReady.js"&gt;&lt;/script&gt;
        &lt;script type="text/javascript" src="js/PluginDetection.js"&gt;&lt;/script&gt;
         &lt;script type="text/javascript" src="js/Silverlight.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript" src="js/jquery-1.2.6.min.js"&gt;&lt;/script&gt;
           &lt;script type="text/javascript" src="js/jquery.flow.1.2.js"&gt;&lt;/script&gt;
            &lt;script type="text/javascript" src="layout/layout/default/js/LayoutInit.js"&gt;&lt;/script&gt;
             &lt;script type="text/javascript" src="php/plugins/MoviePlayer/windows/IE/Silverlight/js/SLPlayer.js"&gt;&lt;/script&gt;
              &lt;script type="text/javascript" src="php/plugins/MoviePlayer/windows/IE/Silverlight/js/wmvplayer.js"&gt;&lt;/script&gt;
             &lt;/head&gt;
              &lt;body onLoad="javascript: GlobalReady();"&gt;
               &lt;div id="container"&gt;
                &lt;div id="header"&gt;
                 &lt;/div&gt;
                &lt;/div&gt;
               &lt;/body&gt;
              &lt;/html&gt;
</code></pre>

<p>Which is a little off of what I am trying to achieve.</p>

<p>I'm not any sort of regular expression expert; or user even, and I assume that I could achieve this in that manner, but wouldn't know how to go about it.</p>

<p>Effectively, my question is this; how would You approach formatting this properly ( programatically ), if you were printing it out one line at a time? Is there a library that could identify all of the elements on a single line, regardless of their association with a later element?</p>

<p>If not I'm sure I'll be writing a new class to handle this on my own.</p>

<p>I am trying to achieve something like this:</p>

<pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" /&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
 &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Your Sitename - Module - Index&lt;/title&gt;
  &lt;link rel="stylesheet" href="layout/layout/default/css/styles.css" type="text/css" /&gt;
  &lt;link rel="stylesheet" href="php/plugins/MoviePlayer/windows/IE/Silverlight/css/silverlight.css" type="text/css" /&gt;
  &lt;script type="text/javascript" src="js/Cookies.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/GlobalReady.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/PluginDetection.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/Silverlight.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/jquery-1.2.6.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/jquery.flow.1.2.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="layout/layout/default/js/LayoutInit.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="php/plugins/MoviePlayer/windows/IE/Silverlight/js/SLPlayer.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="php/plugins/MoviePlayer/windows/IE/Silverlight/js/wmvplayer.js"&gt;&lt;/script&gt;
 &lt;/head&gt;
 &lt;body&gt;
 &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Thanks ahead of time.</p>
