Skip to content

Instantly share code, notes, and snippets.

@SoftCreatR
Last active February 13, 2023 09:11
Show Gist options
  • Save SoftCreatR/cc934f94ad3a046f53b1cebb21e4002f to your computer and use it in GitHub Desktop.
Save SoftCreatR/cc934f94ad3a046f53b1cebb21e4002f to your computer and use it in GitHub Desktop.
RFC 2822 Regex PHP example

EmailValidator Class

The ScEmailValidator class provides a static method for validating email addresses using a regular expression.

The regular expression used by this class is based on the pattern defined in RFC 2822 for email addresses. The regular expression is designed to match a wide range of valid email addresses, including email addresses with domain names that contain IP addresses, internationalized domain names (IDNs), and new top-level domain names. The regular expression also supports email addresses with local parts that contain special characters, such as periods, plus signs, and quoted strings.

To use the ScEmailValidator class, simply call the static validateEmail() method and pass in an email address as a string. The method will return a boolean value that indicates whether the email address is valid or not. If the email address is valid, the method returns true; otherwise, it returns false.

Example usage:

$email = 'user@example.com';

if (ScEmailValidator::validateEmail($email)) {
  echo "$email is a valid email address.";
} else {
  echo "$email is not a valid email address.";
}

Note that the ScEmailValidator class only checks the syntax of the email address and does not verify that the domain name exists or that the email address is associated with a valid mailbox. In practice, you may also want to perform additional checks, such as checking the domain name against a list of known good domains, to ensure that the email address is not being used for spam or other malicious purposes.

<?php
/*
* Copyright by SoftCreatR.dev.
*
* License: https://softcreatr.dev/license-terms
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* The above copyright notice and this disclaimer notice shall be included in all
* copies or substantial portions of the Software.
*/
require_once 'ScEmailValidator.php';
$emails = [
'user@example.com', // A basic email address with a valid domain name.
'user123@example.com', // A valid email address with numbers in the local part.
'user.name@example.com', // A valid email address with a period in the local part.
'user+label@example.com', // A valid email address with a plus sign in the local part.
'user@example.co.uk', // A valid email address with a two-level domain name.
'user@example.museum', // A valid email address with a new top-level domain name.
'user@[192.0.2.255]', // A valid email address with an IPv4 address literal as the domain name.
'user@[IPv6::1]', // A valid email address with an IPv6 address literal as the domain name.
'user@example.com.', // A valid email address with a trailing dot in the domain name.
'user@subdomain.example.com', // A valid email address with a subdomain.
'user@xn--hxajbheg2az3al.xn--jxalpdlp', // A valid email address with an IDN.
'user@123.45.67.89', // A valid email address with an unquoted IPv4 address as the domain name.
'user@example.com-', // A valid email address with a hyphen at the end of the domain name.
'user@example-.com', // A valid email address with a hyphen at the beginning of the domain name.
'user@com', // A valid email address with a two-letter top-level domain name.
'user@example.中国', // A valid email address with an IDN containing non-ASCII characters.
'user@[IPv6:2001:db8::1]', // A valid email address with a long-form IPv6 address literal.
'user@[IPv6::2:1]', // A valid email address with a short-form IPv6 address literal.
'user@very-long-domain-name-example.com', // A valid email address with a domain name longer than 255 characters.
'user@long-sub-domain-name.example.com', // A valid email address with a subdomain longer than 63 characters.
'user@"quoted\"string"@example.com', // A valid email address with a quoted string containing a double quote.
'user@\'quoted\\\'string\'@example.com', // A valid email address with a quoted string containing a single quote.
'"very.unusual.@.unusual.com"@example.com', // A valid email address with a quoted string containing an unusual character.
'user@localhost', // A valid email address with the special domain name "localhost".
'user@com.', // A valid email address with a domain name containing a trailing dot.
'user@example..com', // An invalid email address with two consecutive dots in the domain name.
'user@-example.com', // An invalid email address with a hyphen at the beginning of the domain name.
'user@example.com-', // An invalid email address with a hyphen at the end of the domain name.
'user@exam_ple.com', // An invalid email address with an underscore in the domain name.
'user@exam ple.com' // An invalid email address with a space in the domain name.
];
foreach ($emails as $email) {
echo "$email is " . (EmailValidator::validateEmail($email) ? 'valid' : 'invalid') . PHP_EOL;
}
<?php
/*
* Copyright by SoftCreatR.dev.
*
* License: https://softcreatr.dev/license-terms
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* The above copyright notice and this disclaimer notice shall be included in all
* copies or substantial portions of the Software.
*/
final class EmailValidator
{
private static $regex = '/^(?=.{1,64}@.{1,255}$)'
. '(?:(?:"(?:\\[\x00-\x09\x0B-\x0C\x0E-\x7F]|[^\\"])*")'
. '|(?:\'(?:\\[\x00-\x09\x0B-\x0C\x0E-\x7F]|[^\\\'])*\')'
. '|([a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~-]+))'
. '(?:\.(?:(?:"(?:\\[\x00-\x09\x0B-\x0C\x0E-\x7F]|[^\\"])*")'
. '|(?:\'(?:\\[\x00-\x09\x0B-\x0C\x0E-\x7F]|[^\\\'])*\')'
. '|([a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~-]+)))*'
. '@(?:(?=[a-zA-Z0-9-]{1,63}\.)'
. '[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/';
public static function validateEmail(string $email): bool
{
return (bool) preg_match(self::$regex, $email);
}
}
@SoftCreatR
Copy link
Author

It is known that the regex is not yet perfect and recognizes as invalid some special cases that are valid according to RFC 2822. A fix is in the works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment