PHP Labware internal utilities / htmLawed

HTMLAWED 1.1.1 to prettify HTML like HTML Tidy, or to compact it

Some examples illustrating use of the htmLawed PHP script to pretty-print/beautify HTML, or to compact it. Beautification involves using white spaces like tabs and line-breaks to indent HTML code. Compaction removes unnecessary white spaces. In (X)HTML, except for some tag content, tabs, spaces and line-breaks are considered equivalent, and contiguous white spaces are considered as one.

An example input, and various outputs obtained with different $config['tidy'] values are shown (refer to the documentation for more details). For illustration, tabs are rendered as runs of four spaces, and line-breaks are marked by the character ¬. The PHP code used is:

$out = htmLawed($in, array('tidy'=>'some value'));

Valid but ill-formatted input »
<div>¬
<div>¬
<table¬
border="1"¬
style="background-color: red;">¬
<tr>    <td>A    cell</td><td colspan="2" rowspan="2">¬
<table border="1" style="background-color: green;"><tr><td>Cell</td><td colspan="2" rowspan="2"></td></tr><tr><td>Cell</td></tr><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr></table>¬
</td></tr>¬
<tr><td>Cell</td></tr><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr></table></div></div>

Outputs with various $config['tidy'] values

1 -- default, using two spaces per indent »
<div>¬
  <div>¬
   <table border="1" style="background-color: red;">¬
    <tr>¬
     <td>A cell</td>¬
     <td colspan="2" rowspan="2">¬
     <table border="1" style="background-color: green;">¬
      <tr>¬
       <td>Cell</td>¬
       <td colspan="2" rowspan="2"></td>¬
      </tr>¬
      <tr>¬
       <td>Cell</td>¬
      </tr>¬
      <tr>¬
       <td>Cell</td>¬
       <td>Cell</td>¬
       <td>Cell</td>¬
      </tr>¬
     </table>¬
     </td>¬
    </tr>¬
    <tr>¬
     <td>Cell</td>¬
    </tr>¬
    <tr>¬
     <td>Cell</td>¬
     <td>Cell</td>¬
     <td>Cell</td>¬
    </tr>¬
   </table>¬
  </div>¬
 </div>¬
 

1t1 -- a tab per indent, and a tab of leading indent »
        <div>¬
            <div>¬
                <table border="1" style="background-color: red;">¬
                    <tr>¬
                        <td>A cell</td>¬
                        <td colspan="2" rowspan="2">¬
                        <table border="1" style="background-color: green;">¬
                            <tr>¬
                                <td>Cell</td>¬
                                <td colspan="2" rowspan="2"></td>¬
                            </tr>¬
                            <tr>¬
                                <td>Cell</td>¬
                            </tr>¬
                            <tr>¬
                                <td>Cell</td>¬
                                <td>Cell</td>¬
                                <td>Cell</td>¬
                            </tr>¬
                        </table>¬
                        </td>¬
                    </tr>¬
                    <tr>¬
                        <td>Cell</td>¬
                    </tr>¬
                    <tr>¬
                        <td>Cell</td>¬
                        <td>Cell</td>¬
                        <td>Cell</td>¬
                    </tr>¬
                </table>¬
            </div>¬
        </div>¬
       

-1 -- to compact »
<div><div><table border="1" style="background-color: red;"><tr><td>A cell</td> <td colspan="2" rowspan="2"><table border="1" style="background-color: green;"><tr><td>Cell</td><td colspan="2" rowspan="2"></td></tr><tr><td>Cell</td></tr><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr></table></td></tr> <tr><td>Cell</td></tr><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr></table></div></div>

htmLawed | PHP Labware home | visitors since Sept 2017