www.webdeveloper.com
Results 1 to 6 of 6

Thread: Is PHP confused about FILTER_VALIDATE_EMAIL?

  1. #1
    Join Date
    Sep 2006
    Posts
    655

    Question Is PHP confused about FILTER_VALIDATE_EMAIL?

    HI

    Please consider the following code:

    PHP Code:
    var_dump(filter_var('a=b@test.com',  FILTER_VALIDATE_EMAIL)); 
    Ideally the above email should be invalid and var_dump should return false but the strange thing is when u run this code on the following sites they return non similar results, can anyone tell me whats going on?

    Site: http://codepad.org/MtDCoxxZ
    Output: false

    Site: http://writecodeonline.com/php/
    Output: True

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    What makes you think it's an invalid email address? (You might be surprised what the standard actually allows, though 99.9% or more of in-use email addresses only use a fairly small sub-set of what is allowed.)

    Using one regexp that claims to be RFC-compliant:
    PHP Code:
    <?php

    $regex 
    '#(?:[a-z0-9!\#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!\#$%&\'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])#';

    $test preg_match($regex"a=b@foo.com");
    var_dump($test); // (int) 1
    "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."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2013
    Posts
    31
    Try using this :
    PHP Code:
    $pattern '/[^\pL\pN!#$%&\'\*\+\-\/\=\?\^\_`\{\|\}\~\@\.\[\]]/u';
    $email preg_replace($pattern'''some@email.com'); 
    I've tested it.

  4. #4
    Join Date
    Sep 2006
    Posts
    655
    Quote Originally Posted by The Alchemist View Post
    Try using this :
    PHP Code:
    $pattern '/[^\pL\pN!#$%&\'\*\+\-\/\=\?\^\_`\{\|\}\~\@\.\[\]]/u';
    $email preg_replace($pattern'''some@email.com'); 
    I've tested it.

    Thanks but what will it do?

  5. #5
    Join Date
    Jul 2013
    Posts
    31
    You said you're having issues with filter_var() so, here's something that'll do the work.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    It also depends on why you want to filter_var() it in the first place. Syntax checking is often of limited value (at best). If the user enters a syntactically correct email address which is misspelled, it's not of much use. If you reject a valid email address because your regular expression rejects certain edge cases that are, in fact, valid, it's not only of limited use but can actually lose business for you.

    This is probably why most site registrations just take whatever the user gives them (often using the enter-it-twice method) and sends an email with a custom link to validate the registration. In other words, the only way to truly know if an email address is completely correct is to use it.
    "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."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles