#native_company# #native_desc#
#native_cta#

Custom tag parser

By Armando
on May 6, 2003

Version: 1.2

Type: Class

Category: Other

License: GNU General Public License

Description: This class can parse your custom tags and submits function for it ..

example:
<mytag:tagImage src=”myigame.gif”></mytag:image>

would call your function tagImage

tags can be also written like
<tag=”something”>data</tag>

constants in the start of file define that

<?php

/**
 * My Tag class
 * @author Armando Ota <[email protected]>
 * @version 1.2 
 * @since 28.04.2003, 08:33:49
 * @copyright GPL - jada jada jada
**/


define("MYTAG_PREFIX","rendertag"); 	//prefixes you may use 
define("MYTAG_CONVERT_TO_BR","0");  //converts CR to HTML <BR /> 
define("MYTAG_SEPARATOR",":");		//defines separator



class myTag {
	
	var $tag_stack = null;
	var $basic_string = ""; 	
	
	var $CONVERT_TO_BR = 0;	
	/**
	 * constructor
	 * @return 
	 * @version 1.1 
	 * @author Armando Ota <[email protected]>
	 * @since 28.04.2003, 08:43:02
	 **/
	function myTag(){
		$this->CONVERT_TO_BR=MYTAG_CONVERT_TO_BR;
	}	

	
	/**
	 * definition functions 
	 * INPUT PARAMS ARE NECESSERY !!!
	 * @param Array $attr=null array of attributes you can use with tag
	 * @param String $data="" data you want do parse 
	 * @return String
	 * @version 1.1 
	 * @author Armando Ota <[email protected]>
	 * @since 28.04.2003, 16:37:34
	 **/
	
	
	 
	
	
	
	/**
	 * parser for input string
	 * @param string $str string for parsing
	 * @return string
	 * @version 1.1 
	 * @author Armando Ota <[email protected]>
	 * @since 28.04.2003, 08:43:57
	 **/
	function parseText($str=""){
		if($str){
			$str=" ".$str;
			$start_tag_lok=strpos($str,"<".MYTAG_PREFIX.MYTAG_SEPARATOR);
			while($start_tag_lok){
					
				$def_tag_len=strlen("<".MYTAG_PREFIX.MYTAG_SEPARATOR); //length of start string tag
				$start_tag_lok_end=strpos($str,">",$start_tag_lok+1); // find the closing gap for start tag
				
				//find tag string
				$tag_string=substr($str,$start_tag_lok+$def_tag_len,$start_tag_lok_end-($start_tag_lok+$def_tag_len));
				//echo "tag:".$tag_string;
				// posibility that tag has been written like <tag='something'>
				$start_attr_eq=strpos($tag_string,"="); //find occuarance of '=' sign 
				$start_attr_space=strpos($tag_string," ");  //find occuarance of ' '(whitespace) sign 
				if($start_attr_space && $start_attr_eq){
					if(!$start_attr_space) $start_attr_space=strlen($tag_string);
					if($start_attr_eq<$start_attr_space){
						//if tag conatins equal sign
						$tag_name=substr($tag_string,0,$start_attr_eq);
						$attr_str=trim(substr($tag_string,0,strlen($tag_string)+1));
						//puts it into array
						$attr_arr=explode(" ",$attr_str);
						for($i=0;$i<sizeof($attr_arr);$i++){
							$sec_attr_arr=explode("=",$attr_arr[$i]);
							//attr_stack now has attribute name as key and its value 
							$attr_stack[$sec_attr_arr[0]]=trim(substr($sec_attr_arr[1],1,strlen($sec_attr_arr[1])-2)); 
						}
					} else {
						//defines where tag name ends 
						if($start_attr_space<$start_attr_eq){
							$start_attr_pos=$start_attr_space;
						} else {
							$start_attr_pos=$start_attr_eq;
						}
						//pick tagname
						$tag_name=substr($tag_string,0,$start_attr_pos);
						//gets attributes string
						$attr_str=trim(substr($tag_string,$start_attr_pos+1,strlen($tag_string)-$start_attr_pos+1));
						//puts it into array
						$attr_arr=explode(" ",$attr_str);
						for($i=0;$i<sizeof($attr_arr);$i++){
							$sec_attr_arr=explode("=",$attr_arr[$i]);
							//attr_stack now has attribute name as key and its value stored
							$attr_stack[$sec_attr_arr[0]]=trim(substr($sec_attr_arr[1],1,strlen($sec_attr_arr[1])-2)); 
						}
					}
				} else {
					$tag_name=substr($tag_string,0,$start_tag_lok_end-$def_tag_len);
				}
				//echo $tag_name;
				//data between start and end tags
				$end_tag_lok=strpos($str,"</".MYTAG_PREFIX.MYTAG_SEPARATOR.$tag_name.">");
				if($end_tag_lok){
					$tag_data=substr($str,$start_tag_lok_end+1,$end_tag_lok-$start_tag_lok_end-1);
					$end_tag_len=strlen("</".MYTAG_PREFIX.MYTAG_SEPARATOR.$tag_name.">");
					//builds the whole string (start and end tag) for replacement
					$whole_tag_string=substr($str,$start_tag_lok,($end_tag_lok+$end_tag_len)-$start_tag_lok);
					if(method_exists($this,$tag_name)){ //checks if tag method exists and calls it 
						
						$str=str_replace($whole_tag_string,call_user_func(array("myTag",$tag_name),$attr_stack,$this->parseText($tag_data)),$str);
					} else {
						$str=str_replace($whole_tag_string,$this->parseText($tag_data),$str); //if tag method does not exists then we replace tag with text
					}
				} 
				$start_tag_lok=strpos($str,"<".MYTAG_PREFIX.MYTAG_SEPARATOR,$start_tag_lok+1);
			}
			if($this->CONVERT_TO_BR){
				$str=str_replace(chr(10),"<br />",$str);
			}
		} 
		return $str;
	}
}



?>