All pastes #790551 Raw Edit

Parser class

public php v1 · immutable
·published 2007-11-20 13:45 UTC
rendered paste body
<?php/*** String replacment class* Could be used in a translation system** Could eventually be extended to make a fully featured template parser.** TODO: Add support for : in extra parameter - FIXED* TODO: Add support for {IF} inside {FOREACH} * TODO: Improve parsing routine* TODO: Document better* * String format* Replace tag with simple variable* 'Test 1: {$var}'* Replace tag with object of variable (Only one level supported)* 'Test 2: {$var->object}'* Replace tag with array element (Only one level supported)* 'Test 3:{$var[object]}'* Replace tag with result from function where varibale is parsed as first parameter* 'Test 4: {$var:strlen}' 		This will replace the tag with the content from strlen function. (Any function can be used, but not methods)* Replace tag with result from function using extra parameter* 'Test 5: {$var:date:d-m-y}' 	This will replace tag with content from the date function using 'd-m-y' as the first parameter and value of var as second.* Escaping tag replacment* 'Test 6: {$this text is in braces}  will be removed using the final parser the removes unreplaced tags. only matching braces will be removed not single } or {.* Condition * 'Test 7: {IF $var}Show this text i $var is true{/IF}  This tag is very usefull if you would like to display some content if the variable is true.  You can use the ! in front of $ to invert the conditional. Empty string are determined false* Loop* Test 8: {FOREACH $var}Loop thought an array {$var}{/FOREACH} This tags loop througt a entire array. (You still acceses the variables using the same keyword)  * NOTE:  IF statements inside FOREACH statements is currently not allowed. This will be fixed in later version.*/class Parser {var $org_string;var $output;var $replacement;var $patterns;var $replacefunc;	function Parser() {		//Assign Patterns for replacement		$this->patterns = array(			//IF call			'/{IF (!?)\$([a-zA-Z0-9]+\.?[a-zA-Z0-9]*)}([\w\W]+){\/IF}/',			//Foreach call			'/{FOREACH \$([a-zA-Z0-9]+\.?[a-zA-Z0-9]*)}([\w\W]+){\/FOREACH}/',			//Simple variable replacement	        '/{\$([a-zA-Z0-9]+\.?[a-zA-Z0-9]*)}/', 			//Simple replacment of variable or subvariable (Calling userfunc before output)			'/{\$([a-zA-Z0-9]+\.?[a-zA-Z0-9_]*):([a-zA-Z0-9_]+)}/',			//Replacement of variable using callback function with extra parameter			'/{\$([a-zA-Z0-9]+\.?[a-zA-Z0-9]*):([a-zA-Z0-9_]+):([a-zA-Z0-9_%-: ]+)}/'		);					$this->replacefunc = array(			//IF callback			array('self','replace_if'),			//Foreach callback			array('self','replace_foreach'),			//Simple variable replacement	        array('self','replace_var'), 			//Simple replacment of variable or subvariable (Calling userfunc before output)			array('self','replace_function'),			//Replacement of variable using callback function with extra parameter					array('self','replace_function_para')		);							}	function string($string) {		$this->org_string = $string;		$this->output = $string;	}	function add($tag,$value) {		if(is_object($value)) { $value = get_object_vars($value); }		$this->replacement[$tag] = $value;	}	function batchadd($array) {		foreach($array as $key=>$value) {			$this->add($key,$value);		}	}	function render() {		$this->output = $this->parse($this->output);		return $this->output;	}			private function parse($string) {		for($t=0;$t<count($this->patterns);$t++) {			$string = preg_replace_callback($this->patterns[$t], $this->replacefunc[$t], $string);		}		return $string;	}		//Thsi is a private function which stips all remaing '{' and '}' tags. This way unreplaced tags won't be displayed.	private function striptags($string) {		return preg_replace('/{[a-zA-Z0-9:_\$]*}/','',$string);	}	function var_value($string) {		$parsed = explode('.',$string);		if($parsed[1]) { return $this->replacement[$parsed[0]][$parsed[1]]; }		return $this->replacement[$parsed[0]];	}	function unset_var_value($string) {		$parsed = explode('.',$string);		if($parsed[1]) { unset($this->replacement[$parsed[0]][$parsed[1]]); }		unset($this->replacement[$parsed[0]]);	}		function replace_var($callback) {		return $this->var_value($callback[1]);	}			function replace_function($callback) {		return call_user_func($callback[2],$this->var_value($callback[1]));	}	function replace_function_para($callback) {		return call_user_func($callback[2],$callback[3],$this->var_value($callback[1]));	}	function replace_if($callback) {			if($callback[1]=='') {if($this->var_value($callback[2])) {return $this->parse($callback[3]); }}			if($callback[1]=='!') {if(!$this->var_value($callback[2])) {return $this->parse($callback[3]); }}		return '';	}	function replace_foreach($callback) {		$temp = $this->var_value($callback[1]);		$this->unset_var_value($callback[1]);		$parsed = explode('.',$callback[1]);		foreach($temp as $var) {				if($parsed[1]) { 					$this->replacement[$parsed[0]][$parsed[1]] = $var; 				} else { 					$this->replacement[$parsed[0]] = $var;				}var_dump($this->replacement);			$string .= $this->parse($callback[2]);		}	return $string; 	}		}?>