Return to Snippet

Revision: 13068
at April 8, 2009 15:54 by caioariede


Updated Code
<?php

	class Parser
	{
		public $func_expr = '!\[#(.+?)(?:\s(.+?))?\]!s';
		public $args_expr = '!(\S+)\s*=\s*([\'"]?)([^\2]+?)\2!s';

		public function __construct($text, $cbclass)
		{
			$this->_text = $text;
			$this->_cbclass = $cbclass;
		}

		public function func_expr($expr = NULL)
		{
			return empty($expr) ? $this->func_expr : $this->func_expr = $expr;
		}

		public function args_expr($expr = NULL)
		{
			return empty($expr) ? $this->args_expr : $this->args_expr = $expr;
		}

		public function do_parse()
		{
			return preg_replace_callback($this->func_expr, array($this, 'do_parse_func'), $this->_text);
		}

		private function do_parse_func($m)
		{
			list(, $func, $args) = $m;
			$args = $this->do_parse_args($args);
			return call_user_func($this->_cbclass . '::' . $func, $args);
		}

		private function do_parse_args($str)
		{
			preg_match_all($this->args_expr, $str, $m);
			if (empty($m[3])) return NULL;
			return array_combine($m[1], $m[3]);
		}
	}

	class MyParserCallbacks
	{
		public static function func($args)
		{
			//var_dump($args);
			return 'REGEXFTW';
		}
	}

	$text = '[#func] blabla [#func tamanho="520x75"] teste [#func tamanho="520x75"]';

	$parser = new Parser($text, MyParserCallbacks);
	echo $parser->do_parse();

?>

Revision: 13067
at April 8, 2009 14:15 by caioariede


Updated Code
<?php

	class Parser
	{
		public $func_expr = '!\[#(.+?)(?:\s(.+?))?\]!s';
		public $args_expr = '!\s+(\S+)\s*=\s*([\'"]?)([^\2]+?)\2!s';

		public function __construct($text, $cbclass)
		{
			$this->_text = $text;
			$this->_cbclass = $cbclass;
		}

		public function func_expr($expr = NULL)
		{
			return empty($expr) ? $this->func_expr : $this->func_expr = $expr;
		}

		public function args_expr($expr = NULL)
		{
			return empty($expr) ? $this->args_expr : $this->args_expr = $expr;
		}

		public function do_parse()
		{
			return preg_replace_callback($this->func_expr, array($this, 'do_parse_func'), $this->_text);
		}

		private function do_parse_func($m)
		{
			list(, $func, $args) = $m;
			$args = $this->do_parse_args($args);
			return call_user_func($this->_cbclass . '::' . $func, $args);
		}

		private function do_parse_args($str)
		{
			preg_match_all($this->args_expr, $str, $m);
			if (empty($m[3])) return NULL;
			return array_combine($m[1], $m[3]);
		}
	}

	class MyParserCallbacks
	{
		public static function func($args)
		{
			//var_dump($args);
			return 'REGEXFTW';
		}
	}

	$text = '[#func] blabla [#func tamanho="520x75"] teste [#func tamanho="520x75"]';

	$parser = new Parser($text, MyParserCallbacks);
	echo $parser->do_parse();

?>

Revision: 13066
at April 7, 2009 16:54 by caioariede


Updated Code
<?php

	class Parser
	{
		public $func_expr = '!\[#(\S+)(.*)\]!s';
		public $args_expr = '!\s+(\S+)\s*=\s*([\'"]?)([^\2]+?)\2!s';

		public function __construct($text, $cbclass)
		{
			$this->_text = $text;
			$this->_cbclass = $cbclass;
		}

		public function func_expr($expr = NULL)
		{
			return empty($expr) ? $this->func_expr : $this->func_expr = $expr;
		}

		public function args_expr($expr = NULL)
		{
			return empty($expr) ? $this->args_expr : $this->args_expr = $expr;
		}

		public function do_parse()
		{
			return preg_replace_callback($this->func_expr, array($this, 'do_parse_func'), $this->_text);
		}

		private function do_parse_func($m)
		{
			list(, $func, $args) = $m;
			$args = $this->do_parse_args($args);
			return call_user_func($this->_cbclass . '::' . $func, $args);
		}

		private function do_parse_args($str)
		{
			preg_match_all($this->args_expr, $str, $m);
			if (empty($m[3])) return array(); // no args
			return array_combine($m[1], $m[3]);
		}
	}

	class MyParserCallbacks
	{
		public static function func($args)
		{
			//var_dump($args);
			return 'REGEXFTW';
		}
	}

	$text = 'blabla [#func arg1="abc" arg2=5] teste';

	$parser = new Parser($text, MyParserCallbacks);
	echo $parser->do_parse();

?>

Revision: 13065
at April 7, 2009 10:43 by caioariede


Updated Code
<?php

	class Parser
	{
		public $func_expr = '!\[#(\S+)(.*)\]!';
		public $args_expr = '!\s+(\S+)\s*=\s*([\'"]?)([^\2]+?)\2!';

		public function __construct($text, $cbclass)
		{
			$this->_text = $text;
			$this->_cbclass = $cbclass;
		}

		public function func_expr($expr = NULL)
		{
			return empty($expr) ? $this->func_expr : $this->func_expr = $expr;
		}

		public function args_expr($expr = NULL)
		{
			return empty($expr) ? $this->args_expr : $this->args_expr = $expr;
		}

		public function do_parse()
		{
			return preg_replace_callback($this->func_expr, array($this, 'do_parse_func'), $this->_text);
		}

		private function do_parse_func($m)
		{
			list(, $func, $args) = $m;
			$args = $this->do_parse_args($args);
			return call_user_func($this->_cbclass . '::' . $func, $args);
		}

		private function do_parse_args($str)
		{
			preg_match_all($this->args_expr, $str, $m);
			return array_combine($m[1], $m[3]);
		}
	}

	class MyParserCallbacks
	{
		public static function func($args)
		{
			//var_dump($args);
			return 'REGEXFTW';
		}
	}

	$text = 'blabla [#func arg1="abc" arg2=5] teste';

	$parser = new Parser($text, MyParserCallbacks);
	echo $parser->do_parse();

?>

Revision: 13064
at April 7, 2009 10:38 by caioariede


Initial Code
<?php

	class Parser
	{
		public $func_expr = '!\[#(\S+)(.*)\]!';
		public $args_expr = '!\s+(\S+)\s*=\s*([\'"]?)([^\2]+?)\2!';

		public function __construct($text, $cbclass)
		{
			$this->_text = $text;
			$this->_cbclass = $cbclass;
		}

		public function func_expr($expr = NULL)
		{
			return empty($expr) ? $this->func_expr : $this->func_expr = $expr;
		}

		public function args_expr($expr = NULL)
		{
			return empty($expr) ? $this->args_expr : $this->args_expr = $expr;
		}

		public function do_parse()
		{
			return preg_replace_callback($this->func_expr, array($this, do_parse_func), $this->_text);
		}

		private function do_parse_func($m)
		{
			list(, $func, $args) = $m;
			$args = $this->do_parse_args($args);
			return call_user_func($this->_cbclass . '::' . $func, $args);
		}

		private function do_parse_args($str)
		{
			preg_match_all($this->args_expr, $str, $m);
			return array_combine($m[1], $m[3]);
		}
	}

	class MyParserCallbacks
	{
		public static function func($args)
		{
			//var_dump($args);
			return 'REGEXFTW';
		}
	}

	$text = 'blabla [#func arg1="abc" arg2="5"] teste';

	$parser = new Parser($text, MyParserCallbacks);
	echo $parser->do_parse();

?>

Initial URL
http://caioariede.com/

Initial Description
Parse syntax in a text using preg_replace_callback.

Initial Title
Regex Text Syntax Parser

Initial Tags
regex, php, replace, text

Initial Language
PHP