Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
  1. #1
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Helloooooo

    I'm a bit confused with regular expressions in PHP... I'm trying to use preg_match() to check the validity of the format of a given e-mail address, but the expression I'm trying to match is, I assume, all wrong (as it's not working).

    Could you tell me how I would match the following: a series of lowercase or uppercase letters or symbols or numbers, followed by the @ symbol, followed by more lowercase or uppercase letters or symbols or numbers, followed by a dot, followed by more etc. etc...

    ?
    tekp :cheeky: tekponline.com

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    From a work project:
    PHP Code:
    function validate_email_address($address)
        {
            return 
    preg_match(
                    
    "/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\." .
                    
    "[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9" .
                    
    "])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])" .
                    
    ")\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][" .
                    
    "0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]" .
                    
    "?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(" .
                    
    "2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0" .
                    
    "-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/"$address);
        } 
    Obviously I didn't write that regex.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Okay... that doesn't really tell me much...

    Could you simply check for a string of text, then @ symbol, then text, then ., then text again?

    Is there not a simpler character in the expressions syntax that just means 'an alphanumeric character'?
    tekp :cheeky: tekponline.com

  5. #4
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326

  6. #5
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Okay I've downloaded that and looked at the expression:

    Code:
    (^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$)
    And it makes no sense to me...

    Could you explain what each part means...? Sorry if I'm being slow...

    Thank you
    tekp :cheeky: tekponline.com

  7. #6
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    dfjksfjkl\%%$%$%@sdkljkj.435789728 would be valid by such a simple expression, though. The regex above checks (the hell out of) any e-mail address.

    [a-z0-9] matches any alphanumeric character, or [a-zA-Z0-9] for explicit case insensitivity.

    I didn't write the regex, I found it somewhere. There are tons of e-mail regexs out there but this one has worked fine for me, so I still use it.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  8. #7
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Okay so...

    How come there's a minus at the end of that in the second and third occurences?

    And what's "\." and {2,4} and $?

    And how come there's a plus outside the square brackets on a couple of them?

    If I'm asking too many questions and I should just learn it from scratch tell me
    tekp :cheeky: tekponline.com

  9. #8
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    No, ask away. I learned regexs more as a reference than through tutorials. They're really confusing at first but are extremely powerful.

    [a-z-] means to match a to z as well as a hyphen (-).
    {2,4} means match the preceeding expression two to four times. Example: a{2,4} only matches aa, aaa, and aaaa.
    + means match the expression once or more. a+ matches a, aa, aaa, aaaa, aaaaa, etc.
    \. means a literal period. Normally, . in a regex is a wildcard (matches any one character), so \. escapes it.
    $ means the end of the string to match. So, to put some stuff together, a+$ means the string must end with at least one a. Foo doesn't match, afoo doesn't match, but fooa, fooaa, fooaaa, etc. does.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  10. #9
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    What about the underscore...? sorry, forgot to ask!

    and the ^, does that mean the start of a series of expressions or...?

    and if \. means a period, why does *@ mean an @ sign and not \@?
    tekp :cheeky: tekponline.com

  11. #10
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    The questions are fine, but please edit your post instead of duplicating it.

    I don't think underscore has any special meaning. ^ can mean both the beginning of an expression (opposite of $) or negation. *@ doesn't mean anything in context because @ isn't a special character. * is like +, but it means zero or more, not one or more. Therefore, the expression a* matches foo, fooa, a, a completely empty string, etc.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 07:33 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com