note 69540 deleted from function.preg-match by nlopess
| From: | nlopess@php.net | Date: | Mon, 21 May 2007 19:50:27 +0000 |
| Subject: | note 69540 deleted from function.preg-match by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126806@lists.php.net to get a copy of this message | ||
Note Submitter: Fred Schenk
----
I've seen some regexp's here to validate email addresses. I just wanted to add my own
version which accepts the following formats in front of the @-sign:
1. A single character from the following range: a-z0-9!#$%&*+-=?^_`{|}~
2. Two or more characters from the previous range including a dot but not starting or ending with a
dot
3. Any quoted string
After the @-sign there is the possibility for any number of subdomains, then the domain name and
finally a two letter TLD or one of the TLD's specified.
And finally the check is case insensitive.
I used the info on this page and in the RFCs to come to this format. If anybody sees any possible
improvement I'd be happy to hear from you.
<?php
function isValidEmailAddress($address='') {
$pattern = '/^(([a-z0-9!#$%&*+-=?^_`{|}~]'._
'[a-z0-9!#$%&*+-=?^_`{|}~.]*'._
'[a-z0-9!#$%&*+-=?^_`{|}~])'._
'|[a-z0-9!#$%&*+-?^_`{|}~]|'._
'("[^"]+"))'._
'[@]'._
'([-a-z0-9]+\.)+'._
'([a-z]{2}'._
'|com|net|edu|org'._
'|gov|mil|int|biz'._
'|pro|info|arpa|aero'._
'|coop|name|museum)$/ix';
return preg_match ($pattern, $address);
}
?>
PS: I have the pattern-code on one line, but I had to split it up for this post...I hope it still
works this way but haven't tested it. If the code gives an error try removing all the extra
lines...