All pastes #636703 Raw Edit

Anonymous

public php v1 · immutable
#636703 ·published 2007-07-28 03:50 UTC
rendered paste body
<?php/***	@creator - RyanTheGreat, but who cares?*	@info    - SML parsing engine created for Outwar.com*	@usage   - Construct with a string containing SML language code*	@changes - None;*	@todo    - Change class to static so it need not be instantiated, change internal functions to use static methods*	@todo2   - Use a less greedy method for point calc (could use replaces count if all point values were equal)*/Class SMLParse{	//Instance variables	private $_raw;	private $_maxLength;	private $_parsedString;	private $_pointCost;	private $_error;		/** 	*	@description - Constructs an SML parsing object	*	@input1      - Expected type String, contains the entire string to be parsed.	*	@input2      - Optional maximum length parameter, default is infinite length.	*	@input3      - Optional boolean for stripping control characters from the string default is on	*	@return      - null	*/	public function __construct($strInput, $intMaxLength = NULL, $boolStripControlChars = TRUE)	{		$this -> _raw = $strInput;				if($maxLength != NULL)		{			$this -> _maxLength = (int) intval($intMaxLength);		}				if($this -> checkLength() === TRUE)		{			$this -> _parsedString = $boolStripControlChars ? self::stripControlCharacters($this -> _raw) : $this -> _raw;			$this -> calcPointCost();			$this -> prepareString();			$this -> parseAllowedTags();		}	}			/**	*	@description - Checks supplied data against maxlength argument	*	@input		 - null	*	@return		 - Boolean, true on valid length, false on length error	*/	private function checkLength()	{		if($this -> _maxlength != null)		{			if(strlen($this -> _raw) > $this -> _maxLength)				{				$this -> _error = 'Sorry, your information exceeded the maximum length';				return false;			}			else if(strlen($this -> _raw) <= 0)			{				$this -> _parsedString = 'N/A';				return false;			}			return true;		}		return true;	}		/**	*	@description - Parses the supplied raw string and replaces with formatted HTML	*	@input       - null	*	@return		 - Parsed String containing HTML data	*/	private function parseAllowedTags()	{		$strRegex = array		(			"/\[u\](.*?)\[\/u\]/",			"/\[i\](.*?)\[\/i\]/",			"/\[b\](.*?)\[\/b\]/",			"/\[c\](.*?)\[\/c\]/",			"/\[color=(.*?)\](.*?)\[\/color\]/",			"/\[size=(.*?)\](.*?)\[\/size\]/",			"/\[br\]/"					);				$strReplace = array		(			"<u>$1</u>",			"<i>$1</i>",            "<b>$1</b>",            "<center>$1</center>",            "<font color=\"$1\">$2</font>",            "<font size=\"$1\">$2</font>",            "<br />"		);				$this -> _parsedString = preg_replace($strRegex, $strReplace, $this -> _parsedString);	}		/**	*	@description - Calculates point cost for tag use	*	@input       - null	*	@return		 - Integer point cost	*/	private function calcPointCost()	{		$string     = $this -> _raw;		$totalCost  = '0';		//tag costs		$cost = array('2', '2', '2', '3', '3', '3');//uline, italic, bold, center, color, size				preg_match_all("/\[u\](.*?)\[\/u\]/", $string, $matches);		$totalCost += (int) (intval($cost[0]) * count($matches[0]));				preg_match_all("/\[i\](.*?)\[\/i\]/", $string, $matches);		$totalCost += (int) (intval($cost[1]) * count($matches[0]));					preg_match_all("/\[b\](.*?)\[\/b\]/", $string, $matches);		$totalCost += (int) (intval($cost[2]) * count($matches[0]));				preg_match_all("/\[c\](.*?)\[\/c\]/", $string, $matches);		$totalCost += (int) (intval($cost[3]) * count($matches[0]));				preg_match_all("/\[color=(.*?)\](.*?)\[\/color\]/", $string, $matches);		$totalCost += (int) (intval($cost[4]) * count($matches[0]));		 		preg_match_all("/\[size=(.*?)\](.*?)\[\/size\]/", $string, $matches);		$totalCost += (int) (intval($cost[5]) * count($matches[0]));				$this -> _pointCost = $totalCost;	}		/**	*	@description - Prepares the parsed string for HTML inclusion	*	@input		 - null	*	@return		 - HTML formatted string	*/	private function prepareString()	{		$this -> _parsedString = htmlspecialchars($this -> _parsedString, ENT_QUOTES, 'UTF-8');	}		/**	*	@description - Returns a numeric array first element is formatted string, second is point cost, third is errors	*	@input 		 - null	*	@return		 - The final HTML formatted output	*/	public function returnString()	{		$returnArr    = array();		$returnArr[0] = $this -> _parsedString;		$returnArr[1] = $this -> _pointCost; 		$returnArr[2] = $this -> _error;				return $returnArr;	}		/**	*	@description - Strips control characters from the user input (byte value under 32)	*	@input       - null	*	@return      - String with control characters stripped	*/	public static function stripControlCharacters($strRaw)	{		return preg_replace('/[\x00-\x09\x0b-\x0c\x0e-\x1f]/', '', $strRaw);	}}?>