Version: 1.3
Type: Function
Category: HTML
License: GNU General Public License
Description: This is most simple and easy to use HTML parsing. You define your tags, and this function does the all the work.
<? /* Project: HTML parsing function Author: Virginio E. Reis, [email protected] Rev. History: 1.3 - April 20, 2003 Added the <ELSE> tag to the <IF condition><ENDIF> loop and instructions updated Changed the name of 'setif' to just 'if' Changed the name of the function from 'parse' to 'html' 1.2 - April 13, 2003 Added the <IF condition><ENDIF> tag 1.1 - April 6, 2003 Added templates directory setup support with command html('dir', 'parm1') where parm1 is the directory where templates are. No leading or tralling slashs Added the command to comand list bellow 1.0 - March 21, 2003 Inicial release ******* If you improve this function, please let me know ******* ******* so others can benefit from your improvements too ******* Usage: include this function at the begining of your script. See sintax bellow for for how to use it Sintax: html('parm1', 'parm2', 'parm3') parm1 - mandatory. Can take the values: add - Add a piece of HTML to the output buffer. At first call also initialize the output buffer. assign - Assign the tag name used in HTML to the PHP var flush - Replace the tags with the vars and output the HTML code dir - Set the templates base directory. No leading or tralling slashs if - Set the condition to output some code between the <IF condition> and the <ENDIF> parm2 - mandatory in assign. The name of the tag in the HTML code optional in add when called for the first time (without it, add only start the output buffer). can only be a template. mandatory in add. The directory where templates are. No leading or tralling slashs. mandatory in if. Is the condition to validate. If the condition was added, the code is outputed. Otherwhise that piece of code is skiped. optional in flush. If passed can only be an indexed array ($array['index']). This array will be appended to the tags array. This is to enable language tags to be used and treated as the var tags. The language array can also be named $tags, and then there is no need to pass this parameter as long it is included before calling the flush. parm3 - mandatory in assign as the value to replace the tag defined by parm1 not used on the other commands Tags: Use the format <_name_> and replace name with anything you want as long you assign it with html('assign', 'name', $var) <IF condition>some html code to be outputed if the codition exist <ELSE> output other piece of html code if condition does not exist <ENDIF> usage example: HTML code <IF ouptut> <h1>This is a header h1</h1> <ELSE> <h2>This was supposed to be a header h1<h2> <ENDIF> To output the h1 tag, put the folloing anywhere in your PHP script html('if', 'output'); If you dont do it, the html code will not be outputed if there is no <ELSE> tag. With the <ELSE> tag, the code between the <ELSE> and <ENDIF> will be outputed instead. Future: working on a LOOP tag to fill a table with data form a database table(s); make this a class; */ function html($action, $parm1 = "", $parm2 = "") { global $tags; // set to global to allow an external // language array named $tags if (!is_array($tags)) { // check if the tags array exist $tags = array(); // if not create it } static $root_path; // set the document root var $root_path = $_SERVER['DOCUMENT_ROOT']; // and get it static $tmpl_dir; // templates directory static $started = 0; static $ifarray; // array for conditiona HTML output if ($action == "if") { // falg a condition $ifarray[$parm1] = 1; return; } if ($action == "dir") { // sets the templates directory $tmpl_dir = "$root_path/$parm1"; return; } if ($action == "add") { // add HTML to the buffer if (!$started) { // if the buffer was not started ob_start(); // start it $started = 1; // and set the flag } if($parm1) { include "$tmpl_dir/$parm1"; // if there is code, "send it" to the buffer return; } } if ($action == "assign") { // assign a value to a tag $tags[$parm1] = $parm2; return; } if ($action == "flush") { // flus the buffer $elseflag = 0; // clean the else tag flag if ($parm1) { // if a parameter was passed if (is_array($parm1)) { // see if it is an arra $tags = $tags + $parm1; // and add it to the tags array } } $code = ob_get_contents(); // get buffer contents ob_end_clean(); // stop buffering and clean it /* search for if tags and get the code parts */ $regexp = "/<IF (.*)>(.*)<ENDIF>/Usi"; // set the if pattern to match if (preg_match_all($regexp, $code, $parms)) { // check the code and generate the array // with condition, conditional code and the all code for ($i=0; $i < count($parms[1]); $i++) { // go throug the array $wholecode = $parms[0][$i]; // extract the matching string (the all code), $condition = $parms[1][$i]; // the if condition and $condition = trim($condition); // remove any extra characteres from the condition $ifcode = $parms[2][$i]; // extrat the html code only /* now check for the else tag and get the if and else (if any) parts */ $regexp = "/(.*)<ELSE>(.*)/si"; // set the pattern to match if (preg_match($regexp, $ifcode, $codepieces)){ // check if it exist, get it in the array $ifcode = $codepieces[1]; // and extrat the if code $elsecode = $codepieces[2]; // and the else code $elseflag = 1; // falg that an else pice of code exist } /* replace the whole code with the correct piece */ if ($ifarray[$condition] != 1) { // if the condition was not set if ($elseflag != 1) { // and there is no alternative code $code = str_replace($wholecode, "", $code); // remove the whole code } elseif ($elseflag == 1) { // but if there is an alternative code $code = str_replace($wholecode, "$elsecode", $code); // replace the whole code with the else piece } } elseif ($ifarray[$condition] == 1) { // if the condition was set $code = str_replace($wholecode, "$ifcode", $code); // replace the whole code with the if piece of code } } } /* search and replace normal tags */ $regexp = "/<_(.*)_>/Usi"; // define the string to match if (preg_match_all($regexp, $code, $parms)) { // check the whole html and create an array of tags for ($i=0; $i < count($parms[1]); $i++) { $tag = $parms[1][$i]; $code = str_replace("<_".$tag."_>", $tags[$tag], $code); // replace the tags with array content } } echo $code; // output the HTML code return; } } ?>