Prefer PHP ctype Functions Over Regex

 VERBOSE MODE (still need to figure this out)
  [A-PR-Y0-9]{3}  # Area code prefix
  -
  [A-PR-Y0-9]{3}  # 3-digit exchange
  -
  [A-PR-Y0-9]{4}  # 4-digit suffix

Side by Side Speed Test: Ctype vs. Preg_match

Seconds taken to confirm the 32 byte hexdecimal string contains only hex chars and/or digits

ac556680681677f386aacb8e5acedde3 is the 32 byte hexdecimal string tested
Comparing: ctype_xdigit($md5), and preg_match('/[^a-f0-9]/', $md5)


ctype_xdigit said MD5 contains only hexdecimal chars, in 0.0011921 sec.
preg_match said MD5 contains only hexdecimal chars, in 0.0360012 sec.
preg_match i said MD5 contains only hexdecimal chars, in 0.002861 sec.

Compare PHP filter_var with preg_match clear page

Online Tester of regex patterns

Will call preg_match with this pattern:

 
PHP has Built-in Email Filtering?? Check this out.
PHP filter_var(email) says:

Patterns

RFC2822 Email
/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/
alphabet
[A-Za-z]
other than alphabet
[^A-Za-z]
other than numberic or space
[^0-9 ]
URL
^(https?:\/\/+[\w\-]+\.[\w\-]+)
32 characters all hexdecimal preg()
^[a-f0-9]{32}

Logic for Choosing Regex and not PHP's filter_var

filter_var allows IP domains but not a few of our special characters. We don't allow IP domains as they are mostly used by spammers, and we add a couple of special characters.
    	
    	//return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);//original
    	// general
    	//	- catches double dots in domain names
    	//	- removed "a-z" from character groups as email is case insensitive
    	//return eregi("^[_\.0-9A-Z-]+@([0-9A-Z][0-9A-Z-]+\.)+[A-Z]{2,6}$", $email);
    	// domain part
    	//	- added single letter domains, removed [0-9A-Z] from ([0-9A-Z][0-9A-Z-]+\.)
    	//return eregi("^[_\.0-9A-Z-]+@([0-9A-Z-]+\.)+[A-Z]{2,6}$", $email);
    	//	- disabled 6 letter tlds in favor of 4, but added .museum, catches [email protected]
    	//return eregi("^[_\.0-9A-Z-]+@([0-9A-Z-]+\.)+[A-Z]{2,4}$", $email);
    	//return eregi("^[_\.0-9A-Z-]+@([0-9A-Z-]+\.)+([A-Z]{2,4}|museum)$", $email);
    	//	- could NOT disable capturing w/ereg, switched to preg_match
    	//	- and added ?: to subpattern (?:[0-9A-Z-]+\.)+[A-Z]{2,4}
    	//return preg_match("/^[_\.0-9A-Z-]+@(?:[0-9A-Z-]+\.)+(?:[A-Z]{2,4}|museum)$/i", $email);
    	// local part
    	//	- added whitespace "=", "?", "^", "+", "{", "}", "~", removed escape for dot in char class
    	//return preg_match(/"^[ =?^+{}~_.0-9A-Z-]+@(?:[0-9A-Z-]+\.)+(?:[A-Z]{2,4}|museum)$/i", $email);
    	//	- added no leading, trailing, nor double dots
    	//	- moved dot out of class, and added repeating group
    	return preg_match
    	('/^[ =?^+{}~_0-9A-Z-]+(?:\.[ =?^+{}~_0-9A-Z-]+)*@(?:[0-9A-Z-]+\.)+(?:[A-Z]{2,4}|museum)$/i', $email);
    	//	todo -  64 char limit
  

Site designed and maintained by Freedom Communications | validate xhtml, css