To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > General Help

General Help Forum for General Help questions pertaining to PHP

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 11-12-2008, 11:26 AM   #1
Kerant
Member
 
Kerant's Avatar
 
Join Date: Aug 2008
Location: Preston, UK
Posts: 98
Preg Match Email Address

Hi there,

I was looking around on the PHP Manual for a preg match string that would validate an email address in a form, and came across this:

PHP Code:
If you need to check for .com.br and .com.au and .uk and all the other crazy domain endings i found the following expression works well if you want to validate an email address. Its quite generous in what it will allow

<?php

        $email_address
= "phil.taylor@a_domain.tv";

    if (
preg_match("/^[^@]*@[^@]*\.[^@]*$/", $email_address)) {
        return
"E-mail address";       
    }
       
?>

This string works really well it seems, but I was just wondering if somebody could explain what it is actually doing (the string "/^[^@]*@[^@]*\.[^@]*$/")?

I'm eager to understand how it works and I can't seem to find a decent enough tutorial.

Thanks very much.
Kerant is offline   Reply With Quote
Old 11-12-2008, 11:45 AM   #2
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 12,638
You might want to take a look at this article before settling for that particular regexp solution.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett

Email me
NogDog is offline   Reply With Quote
Old 11-12-2008, 01:10 PM   #3
sfullman
Web Interface Architect
 
Join Date: Jan 2002
Location: Central Texas
Posts: 875
better

I concur, that's a pretty sorry regex for email matching you could get away with murder on that, in fact this string

PHP Code:
@.
would work :-~ - and it would fail for domains like @sub.domain.com

"/^[^@]*@[^@]*\.[^@]*$/" basically means:

start of string
anything besides @ "maybe"
an @
anything besides @ "maybe"
one period
anything besides @ "maybe"

yuck..

here's what I use:
PHP Code:
function valid_email($x){
    if(
preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x))return true;
    return
false;
}
__________________
Hey! You may have inadvertently stumbled onto some genius there! :-)
sfullman is offline   Reply With Quote
Old 11-12-2008, 01:21 PM   #4
Kerant
Member
 
Kerant's Avatar
 
Join Date: Aug 2008
Location: Preston, UK
Posts: 98
Quote:
Originally Posted by sfullman View Post
here's what I use:
PHP Code:
function valid_email($x){
    if(
preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x))return true;
    return
false;
}
Ah that looks good. Could you briefly explain how it works please?

Thanks for the replies!
Kerant is offline   Reply With Quote
Old 11-12-2008, 01:28 PM   #5
sfullman
Web Interface Architect
 
Join Date: Jan 2002
Location: Central Texas
Posts: 875
I couldn't sleep with myself at night knowing I'd denied a newbie their fundamental right to research and learning. NogDog's link above actually shows my function would fail under some cases, so study that but also look up some regex tutorials which there are many online

cheers mate

__________________
Hey! You may have inadvertently stumbled onto some genius there! :-)
sfullman is offline   Reply With Quote
Old 11-12-2008, 05:55 PM   #6
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 12,638
In case you've not yet found these pages in the manual:
Pattern Syntax
Pattern Modifiers
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett

Email me
NogDog is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 03:24 AM.








Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.