RM - Source Code View

Source Code Viewer

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.

Hide Lines Numbers

View: Indenting with htag()

htag() is a function that generate HTML tags and indents the code so it looks pretty in View Source.

Formatted Code

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.

Indenting with htag()

001 <!DOCTYPE html> 002 <!-- This file is created using htag() --> 003 <?php 004 /** 005 * This script will test the htag() function used for indenting HTML 006 * output. htag() calls a notification program msgNote() to report 007 * errors. This test script lives on the page: 008 * @see https://yintercept.com/resources/htag.php 009 * @package ResourceModel 010 * 011 *******************************************************************/
require('/var/www/php/htag.php');
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 ?>
// End Require
012 013 htag('html','lang="en_us"'); 014 // calling with a single tag will open tag and indent. 015 htag('head'); 016 // calling htag with contents tells htag to wrap contents in tags 017 htag('title','','Indenting with htag()'); 018 // begin tag with a dash tells htag to auto-close tage 019 htag('-meta','name="description" content="An article, with code demonstrates indenting html with htag"'); 020 htag('-meta','name="keywords" content="indent php"'); 021 htag('-link','rel="stylesheet" href="rm.css"'); 022 htag('-link','rel="canonical" href="https://yintercept.com/resources/htag.php"'); 023 // calling htag with a slash "/" closes the tag 024 htag('/head'); 025 htag('body'); 026 htag('div','class="main"'); 027 htag('h1','','Indenting with hTag()'); 028 // In this paragraph, I write the paragraph in the contents field of htag() 029 htag('p','','This demonstrates indenting text with the htag() function. To see 030 the output, use the view source command. For most browsers, you can do this by right clicking in the document and selecting "v"iew "s"ource.'); 031 // in this second paragraph, I create the tag, then just echo contents 032 htag('p'); 033 echo 'You can view the PHP program for this page and htag() through my '; 034 // not the goal of htag is to indent the HTML, not to replace tags. 035 // I will just embed a link in the output I echo: 036 echo '<a href="https://yintercept.com/resources/view.php?script=4#code">Source Code Viewer</a>.'; 037 htag('/p'); 038 htag('p'); 039 echo "<b>htag</b>() is a very simple program."; 040 echo "htag('tag'); will open a tag and increase indent level."; 041 echo "htag('-tag'); will create a self closing tag, eg. a meta tag."; 042 echo "htag('/tag'); will close a tag and decrease indent and drop a note in the HTML if there is a tag mismatch."; 043 htag('/p'); 044 htag('p','style="color: #008;"'); 045 echo 'The second parameter is the text for the attribute section of the tag. I turned this paragraph blue with a style attribute in the tag.'; 046 htag('/p'); 047 htag('p','','The $contents parameter is for the contents between an open and close tage. When contents is not null (as it is for the paragraph I am writing), htag() will create a self closing tag. If $contents is an array, the program will enclose each section of the array in tags.'); 048 htag('p','','In the next call, I will pass an array summarizing the last paragraphs.'); 049 // I create a tag a pass it to htag: 050 $arr = ["htag('tag'); opens tag and indents next level.", 051 "htag('-tag'); creates a self closing tag.", 052 "htag('/tag'); closes a tag.", 053 "The attributes parameter sets the tags attributes.", 054 "If the call include contents; htag creates a self closing tag.", 055 "if the contents is an array, htag will enclose the array in tags."]; 056 // Note, if tag is ol or ul, htag will wrap contents in li tags: 057 htag('ul','',$arr); 058 htag('p'); 059 echo 'In this next example, I will produce a select list from a two dimensional array with the third element selected:'; 060 // this is a two dimensional array 061 $arr=array([1,'one'],[2,'two'],[3,'three'],[4,'four'],[5,'five']); 062 // I put the number for selected item in the extra parameter: 063 htag('select','name="name"',$arr,3); 064 htag('/p'); 065 function htagTable($sql) { 066 // 067 } 068 htag('/div'); 069 htag('/body'); 070 htag('/html'); 071 ?>

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 January 20 2020 07:55:31.. This page has been viewed 751 Times.

blog ~ Resource Model ~ links