Location: PHPKode > scripts > QValidation > qvalidation/Validation/DataType/String.php
<?php

class Validation_DataType_String extends Validation implements ValidInterface {

	public static function getInstance(){
		return new Validation_DataType_String;
	}

	/**
	 * validateLengthGreater
	 * @desc checks if a string is longer than the comparison value
	 * @example array("this is a sample string",10)
	 * @param array $value
	 * @return boolean
	 */
	public static function validateLengthLonger(array $value) {
		if(count($value)==2 and is_string($value[0]) and is_integer($value[1])){
			return (strlen($value[0])>$value[1])?true:false;
		}
		return false;
	}

	/**
	 * validateLengthShorter
	 * @desc checks if a string is shorter than the comparison value
	 * @example array("this is a sample string",10)
	 * @param array $value
	 * @return boolean
	 */
	public static function validateLengthShorter(array $value) {
		if(count($value)==2 and is_string($value[0]) and is_integer($value[1])){
			return (strlen($value[0])<$value[1])?true:false;
		}
		return false;
	}

	/**
	 * validateLengthEqual
	 * @desc checks if the length of a string is equal to the length of the comparison value
	 * @example array("this is a sample string",10)
	 * @param array $value
	 * @return boolean
	 */
	public static function validateLengthEqual(array $value) {
		if(count($value)==2 and is_string($value[0]) and is_integer($value[1])){
			return (strlen($value[0])==$value[1])?true:false;
		}
		return false;
	}

	public static function validateUTF8($value) {
		return preg_match ( '@^(?:
          [\x09\x0A\x0D\x20-\x7E]            # ASCII
        | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
        |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
        | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
        |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
        |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
        | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
        |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
  		)*$@x', $value);
	}

	/**
	 * validateIp
	 * @param string $value
	 * @return bool
	 */
	public static function validateIp($value) {
		if(substr_count($value, ".")==3) {
			$regex ='/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/';
			return (preg_match($regex, $value)) ? true : false;
		}
		return false;
	}

	/**
	 * validatePostCode
	 * @param string $value
	 * @param string $countryCode
	 * @return boolean
	 */
	public static function validatePostCode($value, $countryCode = 'de') {
		$regexes=array(
           'at'=>'/[0-9]{4,4}/',
           'au'=>'/[2-9][0-9]{2,3}/',
           'ca'=>'/[a-zA-Z].[0-9].[a-zA-Z].\s[0-9].[a-zA-Z].[0-9]./',
           'de'=>'/[0-9]{5,5}/',
           'ch'=>'/[0-9]{4,4}/',
           'ee'=>'/[0-9]{5,5}/',
           'nl'=>'/[0-9]{4,4}\s[a-zA-Z]{2,2}/',
           'it'=>'/[0-9]{5,5}/',
           'pt'=>'/[0-9]{4,4}-[0-9]{3,3}/',
           'se'=>'/[0-9]{3,3}\s[0-9]{2,2}/',
           'uk'=>'/([A-Z]{1,2}[0-9]{1}[0-9A-Z]{0,1}) ?([0-9]{1}[A-Z]{1,2})/',
           'us'=>'/[0-9]{5,5}[\-]{0,1}[0-9]{4,4}',
		   'ar'=>'/([A-HJ-TP-Z]{1}\d{4}[A-Z]{3}|[a-z]{1}\d{4}[a-hj-tp-z]{3})/',
           );

       	if(array_key_exists($countryCode,$regexes)){
            return preg_match($regexes[$countryCode],$value);
		} else {
			return false;
		}
	}

	/**
	 * validateUrl
	 * @TODO: manipulate $check["path"] And $check["query"] via parse_str
	 * $checks = array("scheme"=>false, "host"=> false, "port" => false, "user" => false, "pass" => false, "path" => false, "query" => false, "fragment" => false);
	 * @param string $value
	 * @return mixed
	 */
	public static function validateUrl($value) {

		$count = 0;
		$url = parse_url($value);
		
		foreach($url as $key => $item) {
			switch($key) {
				case 'scheme':
					switch(strtolower($item)) {
						case 'http':
						case 'https':
						case 'ftp':
							break;
						default:
							return strtoupper("validateUrl_scheme");
					}
					break;
				case 'host':
					$domainSuffix = array(
						"ac","ac.uk","ad","ae","af","ag","ai","al","am","an","ao","aq","ar","arpa","as","at",
						"au","aw","az","ba","bb","bd","be","bf","bg","bh","bi","bj","bm","bn","bo",
						"br","bs","bt","bv","bw","by","bz","ca","cc","cd","cf","cg","ch","ci","ck",
						"cl","cm","cn","co","co.uk","com","cr","cs","cu","cv","cx","cy","cz","de",
						"dj","dk","dm","do","dz","ec","edu","ee","eg","eh","er","es","et","fi","fj",
						"fk","fm","fo","fr","ga","gd","ge","gf","gg","gh","gi","gl","gm","gn","gov",
						"gp","gq","aero","asia","biz","coop","eu","info","museum","name","pro","travel",
						"gr","gs","gt","gu","gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","im","in",
						"int","io","iq","ir","is","it","je","jm","jo","jp","ke","kg","kh","ki","km","kn","kp",
						"kr","kw","ky","kz","la","lb","lc","li","lk","lr","ls","lt","lu","lv","ly","ma","mc",
						"md","mg","mh","mil","mk","ml","mm","mn","mo","mp","mq","mr","ms","mt","mu","mv","mw",
						"mx","my","mz","na","nc","ne","net","nf","ng","ni","nl","no","np","nr","nt","nu","nz",
						"om","org","pa","pe","pf","pg","ph","pk","pl","pm","pn","pr","ps","pt","pw","py","qa",
						"re","ro","ru","rw","sa","sb","sc","sd","se","sg","sh","si","sj","sk","sl","sm","sn",
						"so","sr","sv","st","sy","sz","tc","td","tf","tg","th","tj","tk","tm","tn","to","tp",
						"tr","tt","tv","tw","tz","ua","ug","uk","um","us","uy","uz","va","vc","ve","vg","vi",
						"vn","vu","wf","ws","ye","yt","yu","za","zm","zw"
					);
					$invalidIp = self::validateIp($item);
					$invalidHost = false;
					$invalidDomainSuffix = false;
					if (empty($invalidIp)) {
						$host = explode(".", $item,3);
						$arrDom = array_flip($domainSuffix);
						$invalidDomainSuffix = array_key_exists($host[2], $arrDom);
						
						if (false===($invalidDomainSuffix)) {
							return strtoupper("validateUrl_invalidDomainSuffix");
						}
					}
					if (empty($invalidIp) && empty($invalidDomainSuffix)) {
						return strtoupper("validateUrl_invalidIp");
					}
					break;
			}
			if(1===count($url) and isset($url["path"])) {
				return strtoupper("validateUrl_missingHost");
			}
			$count++;
		}
		
		return ($count>1?true:false);
	}

	/**
	 * validateMacAddress
	 * @param string $value
	 * @return bool
	 */
	public static function validateMacAddress($value){
		$regex = '/([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])/';
		return preg_match($regex, $value);
	}

	public static function validateHashMD5($value){
		$regex = '/([a-z0-9]{32})/';
		return preg_match($regex, $value);
	}

	/**
	 * various email checks
	 */
	public static function validateEmail($value){
		$check = preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $value);
		if(!$check){
			$check = self::validateEmail2($value);
			if(!$check){
				return self::validateEmailStrict($value);
			}
		}
		return $check;
	}

	public static function validateEmail2($value) {
		$arrInvalidChars = array();
		$check = explode('@',$value);		
		if(is_array($check) and isset($check[1])){
			$check2 = explode('.',$check[1]);
			if(count($check) > 2 || strlen($check2[1]) < 2 )	{
				return false;
			}
			if(strstr($value, $arrInvalidChars)) {
				return false;
			}
			return true;
		} else {
			return true;
		}
		return false;
	}

	public static function validateEmailStrict($value) {
		$pattern = '/^ [\w.!#%&\*\/=\?\^\`\{\|\}\~+-]{1,64} \@ [[:alnum:].-]{1,255} \. [a-z]{2,6} $/xi';
		return preg_match($pattern, $value);
	}

	public static function validateISBN10($value) {		
		$isbn = self::vendor("ISBN");
		$isbn->set_isbn10($value);
		return $isbn->valid_isbn10($value);
	}

	public static function validateISBN13($value) {
		$isbn = self::vendor("ISBN");
		$isbn->set_isbn13($value);
		return $isbn->valid_isbn13($value);
	}

	public static function validateGTIN14($value) {
		$isbn = self::vendor("ISBN");
		$isbn->set_gtin14($value);
		return $isbn->valid_gtin14($value);
	}

}
Return current item: QValidation