Regular expresions...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i don't know of any good tutorials.. at least not for php... if you're interested inperl, the tutorial int he orielly learning perl book was the most understandable one i've ever seen.. thing is i now use that until i go back after and make adjustments to whatever language i'm suppossed to be in
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

I found this one tutorial I really did understand: http://www.sitepoint.com/article/974/

The only thing is, the script validates the email address if you enter 2 dot's like so: info@mandio..com

I dunno how to add an extra bit in there to say only allow 1 dot:

here's my code:

Code: Select all

else{
		if(!ereg("^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z.]{2,5}$", $F_email)){
		$errors[] = "You have entered an invalid <b>e-mail</b> address";
		}
I still don't really get certain bits about the preceding characters but I am getting there. I have always been very scared of regular expressions since I began my programming career 5 years ago using ASP and I just used to use a simple replace function... :P
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i started programming by manipulating programs written in basic/truebasic for the appple2e as a child... really got into it in college.. first thing i formally learned was java/c (both concurrently and 5 yrs ago now)

i'm hesitant to call php/asp programming languages because of how they are made in comparison to what i think of as programming languages...

i know there's something in php to validate e-mail addresses. check out the oreilly books. it's in the cookbook...brb
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

ok.. here it is. pg 352-355....

section 13.5


problem: checking to see if an e-mail is valid

solution: there's no way to be completely sure without trying the address, but to weed out typos, there's several patterns. if the IMAP extension is enabled, you can use imap_rfc822_parse_adrlist():

Code: Select all

$parsed=imap_rfc822_parse_adrlist($email_address, $default_host);
if('INVALID_ADDRESS'==$parsed['mailbox']{
 #badaddress
}
the pattern they suggest:
/^[^@\s+@([-a-z0-9]+\.)+[a-z]{2,}$/i

they other pattern they give is

/^[^@\s]+@([-a-z0-9]+\.)+([a-z]{2}|com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum)$/ix

both of their patters only really look at what's after the at. the pattern i made, which seems to do what you want, also checks the begining has no spaces (both of these don't from the write up)

if(!(preg_match('/[\w\.\-]+@[\w\.\-]+\.\w\w\w?/', $email))){$err=TRUE; $errs[]='Your E-Mail address does not appear to be valid'; $step=2;}

that uses perl's regular expression instead of the posix one.

it looks for any number of the characters in \w (A-Z,a-z,0-9,_) . and - repreated any number of times, followed by an @ followed by the first thing again until .\w\w with an optional 3rd, so that it gets all the 2 letter names and 3 letter names.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i hope that helps you some
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

Thank You.
Post Reply