_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
(
"$1",
"$1",
"$1",
"
$1",
"$2",
"$2",
"
"
);
$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);
}
}
?>