3 * EmailAddressValidator Class
5 * @author Dave Child <dave@addedbytes.com>
6 * @link http://code.google.com/p/php-email-address-validation/
7 * @license http://www.opensource.org/licenses/bsd-license.php
8 * @version SVN r10 + Issue 15 fix + Issue 12 fix
10 class EmailAddressValidator {
12 * Set true to allow addresses like me@localhost
14 public $allowLocalAddresses = false;
17 * Check email address validity
18 * @param strEmailAddress Email address to be checked
19 * @return True if email is valid, false if not
21 public function check_email_address($strEmailAddress) {
23 // If magic quotes is "on", email addresses with quote marks will
24 // fail validation because of added escape characters. Uncommenting
25 // the next three lines will allow for this issue.
26 //if (get_magic_quotes_gpc()) {
27 // $strEmailAddress = stripslashes($strEmailAddress);
30 // Control characters are not allowed
31 if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $strEmailAddress)) {
35 // Check email length - min 3 (a@a), max 256
36 if (!$this->check_text_length($strEmailAddress, 3, 256)) {
40 // Split it into sections using last instance of "@"
41 $intAtSymbol = strrpos($strEmailAddress, '@');
42 if ($intAtSymbol === false) {
43 // No "@" symbol in email.
46 $arrEmailAddress[0] = substr($strEmailAddress, 0, $intAtSymbol);
47 $arrEmailAddress[1] = substr($strEmailAddress, $intAtSymbol + 1);
49 // Count the "@" symbols. Only one is allowed, except where
50 // contained in quote marks in the local part. Quickest way to
51 // check this is to remove anything in quotes. We also remove
52 // characters escaped with backslash, and the backslash
54 $arrTempAddress[0] = preg_replace('/\./'
56 ,$arrEmailAddress[0]);
57 $arrTempAddress[0] = preg_replace('/"[^"]+"/'
60 $arrTempAddress[1] = $arrEmailAddress[1];
61 $strTempAddress = $arrTempAddress[0] . $arrTempAddress[1];
62 // Then check - should be no "@" symbols.
63 if (strrpos($strTempAddress, '@') !== false) {
68 // Check local portion
69 if (!$this->check_local_portion($arrEmailAddress[0])) {
73 // Check domain portion
74 if (!$this->check_domain_portion($arrEmailAddress[1])) {
78 // If we're still here, all checks above passed. Email is valid.
84 * Checks email section before "@" symbol for validity
85 * @param strLocalPortion Text to be checked
86 * @return True if local portion is valid, false if not
88 protected function check_local_portion($strLocalPortion) {
89 // Local portion can only be from 1 to 64 characters, inclusive.
90 // Please note that servers are encouraged to accept longer local
91 // parts than 64 characters.
92 if (!$this->check_text_length($strLocalPortion, 1, 64)) {
95 // Local portion must be:
96 // 1) a dot-atom (strings separated by periods)
98 // 3) an obsolete format string (combination of the above)
99 $arrLocalPortion = explode('.', $strLocalPortion);
100 for ($i = 0, $max = sizeof($arrLocalPortion); $i < $max; $i++) {
101 if (!preg_match('.^('
102 . '([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]'
103 . '[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]{0,63})'
105 . '("[^\\\"]{0,62}")'
107 ,$arrLocalPortion[$i])) {
115 * Checks email section after "@" symbol for validity
116 * @param strDomainPortion Text to be checked
117 * @return True if domain portion is valid, false if not
119 protected function check_domain_portion($strDomainPortion) {
120 // Total domain can only be from 1 to 255 characters, inclusive
121 if (!$this->check_text_length($strDomainPortion, 1, 255)) {
125 // some IPv4/v6 regexps borrowed from Feyd
126 // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479
127 $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])';
128 $hex_digit = '[A-Fa-f0-9]';
129 $h16 = "{$hex_digit}{1,4}";
130 $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet";
131 $ls32 = "(?:$h16:$h16|$IPv4Address)";
133 "(?:(?:{$IPv4Address})|(?:".
134 "(?:$h16:){6}$ls32" .
135 "|::(?:$h16:){5}$ls32" .
136 "|(?:$h16)?::(?:$h16:){4}$ls32" .
137 "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32" .
138 "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32" .
139 "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32" .
140 "|(?:(?:$h16:){0,4}$h16)?::$ls32" .
141 "|(?:(?:$h16:){0,5}$h16)?::$h16" .
142 "|(?:(?:$h16:){0,6}$h16)?::" .
143 ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)";
145 // Check if domain is IP, possibly enclosed in square brackets.
146 if (preg_match("/^($IPv4Address|\[$IPv4Address\]|\[$IPv6Address\])$/",
150 $arrDomainPortion = explode('.', $strDomainPortion);
151 if (!$this->allowLocalAddresses && sizeof($arrDomainPortion) < 2) {
152 return false; // Not enough parts to domain
154 for ($i = 0, $max = sizeof($arrDomainPortion); $i < $max; $i++) {
155 // Each portion must be between 1 and 63 characters, inclusive
156 if (!$this->check_text_length($arrDomainPortion[$i], 1, 63)) {
159 if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|'
160 .'([A-Za-z0-9]+))$/', $arrDomainPortion[$i])) {
163 if ($i == $max - 1) { // TLD cannot be only numbers
164 if (strlen(preg_replace('/[0-9]/', '', $arrDomainPortion[$i])) <= 0) {
174 * Check given text length is between defined bounds
175 * @param strText Text to be checked
176 * @param intMinimum Minimum acceptable length
177 * @param intMaximum Maximum acceptable length
178 * @return True if string is within bounds (inclusive), false if not
180 protected function check_text_length($strText, $intMinimum, $intMaximum) {
181 // Minimum and maximum are both inclusive
182 $intTextLength = strlen($strText);
183 if (($intTextLength < $intMinimum) || ($intTextLength > $intMaximum)) {