Scripts

FilterLib

Om mijn vorige post (validatie) compleet te maken wil ik ook graag nog een FilterLib toevoegen. Deze lijkt erg veel op de CheckLib met het verschil dat de FilterLib een meegestuurde string stript van ongewenste tekens. Heb deze class snel in elkaar gezet, deze is niet helemaal compleet, maar geeft denk ik wel mooi aan wat de mogelijkheden zijn :). Voorbeeld: $p_sString = 'straatnaam 34'; echo FilterLib::filter_digits($p_sString);. Geeft 34 terug. Ik wil deze class gebruiken als aanvulling op de inputvalidator.

FilterLib.class.php
<?php
/**
 * @author Mark Eilander
 * @copyright 2010
 * @package onderzoeksinstrument
 * @version 1
 */
 
/**
 * FilterLib
 * @access public
 * @abstract
 */
abstract class FilterLib {
	
	const FILTER_WHITESPACE_ON = 1;
	const FILTER_WHITESPACE_OFF = 2;

	/**
	 * strip_tags Strips all HTML tags and comments from the submitted string
	 * @todo validate the optional parameter with type <p>, <b>, <style> etc
	 * @static
	 * @access public
	 * @param string $p_sString
	 * @param string $p_sParameters
	 * @return Filtered string $p_sString
	 */
	public static function strip_tags($p_sString, $p_sParameter = '') {
		// typecast de waarde naar een string
		$p_sString = (string) $p_sString;
		$p_sParameter = (string) $p_sParameter;
		
		// controleer of de waarde een string is en niet leeg
		if (trim($p_sString) != '') {
			// Are the optional paramets set?
			if (trim($p_sParameter) != '') {
				// Apply filter with optional parameter
            	return strip_tags($p_sString, $p_sParameter);
			} else {
				// Apply filter
            	return strip_tags($p_sString);
			} 
        } else {
        	$p_sString = '';
        	return trim($p_sString);
        }
	}
	
	/**
	 * Returns the string $value, removing all but digit characters
	 * @static
	 * @access public
	 * @param string $p_sString
	 * @return Filtered string $p_sString
	 */
	public static function filter_digits($p_sString) {
		// set the pattern
 		$pattern = '/[^0-9]/';
		// return the filtered string
        return preg_replace($pattern, '', (string) $p_sString);
	}
	
	/**
 	 * Returns the string $value, removing all but alphabetic characters
	 * @static
	 * @access public
	 * @param string $p_sString
	 * @param boolean $p_bWhitespace
	 * @return Filtered string $p_sString
	 */
	public static function filter_alpha($p_sString, $p_bWhitespace = self::FILTER_WHITESPACE_OFF) {
		// check whether whitespaces are allowed
		if ($p_bWhitespace === self::FILTER_WHITESPACE_ON) {
			$p_sWhitespace = '\s';
		} else {
			$p_sWhitespace = '';
		}
		// set the pattern
 		$pattern = '/[^a-zA-Z' . $p_sWhitespace . ']/';
		// return the filtered string
        return preg_replace($pattern, '', (string) $p_sString);
	}
	
	/**
 	 * Returns the string $value, removing all but alphabetic and digit characters
	 * @static
	 * @access public
	 * @param string $p_sString
	 * @param boolean $p_bWhitespace
	 * @return Filtered string $p_sString
	 */
	public static function filter_alphaNum($p_sString, $p_bWhitespace = self::FILTER_WHITESPACE_OFF) {
		// check whether whitespaces are allowed
		if ($p_bWhitespace === self::FILTER_WHITESPACE_ON) {
			$p_sWhitespace = '\s';
		} else {
			$p_sWhitespace = '';
		}
		// set the pattern
 		$pattern = '/[^a-zA-Z0-9' . $p_bWhitespace . ']/';
		// return the filtered string
        return preg_replace($pattern, '', (string) $p_sString);
	}
}
?>

Reacties

0
Nog geen reacties.