From 0a1d30bfaabbfda6df7f79e1d0d5f616a9a2f897 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 25 Jan 2007 15:34:21 +0100 Subject: improve and centralise email regexp, fixes FS#1049 darcs-hash:20070125143421-9b6ab-93ce6bfd804924af4651da9424738494f11fe5f2.gz --- inc/mail.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'inc/mail.php') diff --git a/inc/mail.php b/inc/mail.php index a596c38f7..02434cf3c 100644 --- a/inc/mail.php +++ b/inc/mail.php @@ -144,14 +144,23 @@ function mail_encode_address($string,$header='',$names=true){ * Uses a regular expresion to check if a given mail address is valid * * May not be completly RFC conform! + * @link http://www.faqs.org/rfcs/rfc2822.html (paras 3.4.1 & 3.2.4) * - * @link http://www.webmasterworld.com/forum88/135.htm + * @author Chris Smith * * @param string $email the address to check * @return bool true if address is valid */ + +// patterns for use in email detection and validation +// NOTE: there is an unquoted '/' in RFC2822_ATEXT, it must remain unquoted to be used in the parser +// the pattern uses non-capturing groups as captured groups aren't allowed in the parser +// select pattern delimiters with care! +if (!defined('RFC2822_ATEXT')) define('RFC2822_ATEXT',"0-9A-Za-z!#$%&'*+/=?^_`{|}~-"); +if (!defined('PREG_PATTERN_VALID_EMAIL')) define('PREG_PATTERN_VALID_EMAIL', '['.RFC2822_ATEXT.']+(?:\.['.RFC2822_ATEXT.']+)*@(?:[0-9A-Za-z][0-9A-Za-z-]*\.)+[A-Za-z]{2,4}'); + function mail_isvalid($email){ - return eregi("^[0-9a-z]([+-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email); + return preg_match('<^'.PREG_PATTERN_VALID_EMAIL.'$>', $email); } /** -- cgit v1.2.3