This page lets you view source code from my server. The program uses a brute force code formatter to color code elements. NOTE: I wrote this program while trying to learn the vim text editor. This is not my usual coding style.
Use this select box to select a file.
htag is a program that keeps track of html tags and indents the HTML output.
Below is the code all formatted with bright colors. The program links to files opened with include() and expands those opened with require(). Clicking on the require line should change visibility. You can view the database schema with schema viewer.
htag
001 <?php
002 /**
003 * htag() creates and indents HTML tags.
004 * The function demonstrates using a function to encapsulate the output
005 * buffer.
006 * @author Kevin Delaney
007 * @package ResoureModel
008 * @copyright 2015 kd
009 * @license https://yintercept.com/resources/license.html
010 * For a demonstration page @see https://yintercept.com/resources/htag.php
011 *
012 * @param string $tag contains directives and html tags.
013 * @param string $attribs is the HTML attribute
014 * @param string $oontents is the information between tags
015 * @param extra has extra information for the tag.
016 */
017 // const TAB = "\t"; // I wanted to use a tab, but the default tab in Chrome is 8 characters!
018 // const TAB = ' '; // Set tab to two spaces.
019 function htag($tag='', $attribs='', $contents=NULL, $extra=NULL) {
020 static $level =0;
021 static $tagArray = array();
022 // the delimiter value is PHP_EOL followed by the indent level for the tag.
023 static $eol = '';
024
025 static $rowCnt = 0;
026
027 // if the $tag is empty return current tab and break from the function.
028 if ($tag=='') {
029 if ($contents != null) {
030 echo $eol.str_replace("\n",$eol,$contents);
031 }
032 // an ungraceful exit in mid function.
033 return $eol;
034 }
035
036 // We need to add a space between the tag and attributes.
037 $aSpace = ($attribs == '')? '' : ' ';
038 if (substr($tag,0,1) == '-') {
039 if ($tag=='-dump') {
040 echo PHP_EOL.'<pre>level '.$level.PHP_EOL;
041 print_r($tagArray);
042 echo '</pre>';
043 } else {
044 // use a dash "-" + tag creates a self closing tag at the current level.
045 echo $eol.'<'.substr($tag,1).$aSpace.$attribs.' />';
046 }
047 } elseif (substr($tag,0,1) == '/') {
048 // a slash says close tag and decrement level.
049 // You can close a string of tags as follows:
050 // htag('/table/form/article');
051 $tgArr = explode('/',$tag);
052 $tgCnt = count($tgArr);
053 // echo '';
054 for ($itg = 1; $itg< $tgCnt; $itg++) {
055 $tag = $tgArr[$itg];
056 if (isset($tagArray[$level])) {
057 // a single slash closes the current tag, whatever it is.
058 if ($tag == '') {
059 $tag = $tagArray[$level];
060 } else {
061 // a slash followed by a tag issues a warning if you closed wrong tag.
062 // $tag = substr($tag,1);
063 if ($tagArray[$level] != $tag) {
064 echo $eol.'<!-- Close Tag Mismatch:'.$tag.'/'.$tagArray[$level].'-->';
065 }
066 }
067 unset($tagArray[$level]);
068 if ($level > 0) $level--;
069 $eol = PHP_EOL.str_repeat(TAB,$level);
070 echo $eol.'</'.$tag.'>';
071
072 } else {
073 // The programmer closed too many tags.
074 echo '<!--Dear Programmer, you closed too many tags-->';
075 }
076 }
077 } else {
078 // htag behaves differently for different types of contents.
079 // if you pass an array , it creates multiple tags.
080 if (is_array($contents)) {
081 // tags like tr, ol, ul and select have inner tags like td, li or option.
082 $closeOuter = '';
083 $ieol = $eol; // ieol might be deeper than eol.
084 // some tags naturally contain inner tags, such as ul and tr
085 if ($tag == 'ol' or $tag=='ul') {
086 echo $eol.'<'.$tag.$aSpace.$attribs.'>';
087 $ieol = $eol.TAB;
088 $closeOuter = $eol.'</'.$tag.'>';
089 $tag = 'li';
090 // hrh is a made up tag to denote table header row.
091 } elseif ($tag=='trd') {
092 if ($extra=='alt') {
093 $attribs .= (($rowCnt++ % 2) == 0)? ' class="rowone"' : ' class="rowtwo"';
094 }
095 echo $eol.'<tr'.$aSpace.$attribs.'>';
096 $closeOuter = '</tr>';
097 $ieol=''; // I like table cells to appear on a single line.
098 $tag = 'td';
099 } elseif ($tag=='trh') {
100 // a made for a row containing headers.
101 echo $eol.'<tr'.$aSpace.$attribs.'>';
102 $ieol='';
103 $closeOuter = '</tr>';
104 $tag='th';
105 } elseif ($tag=='select') {
106 echo $eol.'<select ',$attribs.'>';
107 $ieol .= TAB;
108 $closeOuter = $eol.'</select>';
109 $tag = 'option';
110 }
111 $vx=count($contents);
112 for ($vi=0; $vi<$vx; $vi++) {
113 $val=$contents[$vi];
114
115 if (is_array($val)) {
116 //print_r($val);
117 if ($tag == 'option') {
118 // assume inner array is of form [val,display]
119 $selected = ($val[0] == $extra)? ' selected' : '';
120 echo $ieol.'<option val="'.$val[0].'"'.$selected.'>'.$val[1].'</option>';
121 } elseif ($tag=='td') {
122 // assume inner tag is [attrib,display]
123 echo '<td '.$val[0].'>'.$val[1].'</td>';
124 } elseif ($tag=='li') {
125 echo $ieol.'<li '.$val[0].'>'.$val[1].'</li>';
126 }
127 } else {
128 echo $ieol.'<'.$tag.'>'.$val.'</'.$tag.'>';
129 }
130 }
131 echo $closeOuter;
132 } else {
133 // this is where we handle normal tags that open an html section
134 // If contents is null, we produce a tag and increase the indent level.
135 // if contents is not null, we wrap the contents in open and close tags.
136 if (is_null($contents)) {
137 // set a new tag.
138 if (isset($tagArray[$level])) $level++; // the only tag that should not be set is zero.
139 $tagArray[$level] = $tag;
140 echo $eol.'<'.$tag.$aSpace.$attribs.'>';
141 $eol = PHP_EOL.str_repeat("\t",$level);
142 } else {
143 // if contents is set, produce the contents enclosed by an open and closed tag.
144 if (gettype($contents)=='string') {
145 // if tag is a space just print the indented contents.
146 echo ($tag==' ')? $eol.str_replace("\n",$eol,$contents) : $eol.'<'.$tag.$aSpace.$attribs.'>'.$contents.'</'.$tag.'>';
147 } else {
148 echo PHP_EOL.'<!- Error tag '.$tag.'-->';
149 }
150 }
151 }
152 }
153 }
154 function hPC($prompt, $cell) {
155 hTag('tr');
156 hTag('td','class="prompt"',$prompt);
157 hTag('td','class="cell"',$cell);
158 hTag('/tr');
159 }
160
161 ?>
Use "view source" from your browser to grab the output. Feel free to link to this project and check out the Resource Model for information on PHP coding or my tumblr blog for picture of Arizona, Colorado or Utah.
File last modified at April 17 2026 09:23:06.. This page has been viewed 170 Times.
| Record of Revisions | ||||
|---|---|---|---|---|
| id | Rev | by | Date | MD5 Hash |
| 5 | 0.0 | 3 | 2016-07-19 | 656915b53d861ae29ea780b7b33a7fd4 |
| Copied code from yintercept.com | ||||