2010-09-16 9 views
31

Dopo aver letto vari post, ho deciso di non utilizzare REGEX per verificare se un'e-mail è valida e utilizzare semplicemente la funzione filter_var incorporata di PHP. Sembrava funzionare bene, fino a quando non ha iniziato a dirmi che un'email non era valida perché avevo un numero.Il file filter_var di PHP FILTER_VALIDATE_EMAIL funziona effettivamente?

ie [email protected] funziona, mentre [email protected] no.

Mi manca qualcosa o il filter_var($email, FILTER_VALIDATE_EMAIL) è davvero inefficace?

+0

test è inoltre necessario verificare l'esistenza di un punto nell'indirizzo di posta elettronica. http://www.electrictoolbox.com/php-email-validation-filter-var-updated/ –

+0

@willdanceforfun La tua domanda è inesatta. 'name2 @ domain.com' funziona bene su tutte le versioni di PHP che supportano' filter_var() '. Prova: http://3v4l.org/joLvm – Brad

risposta

33

L'espressione regolare utilizzata nel codice del filtro PHP 5.3.3 è basata sul blog di Michael Rushton su Email Address Validation. Sembra funzionare per il caso che hai citato.

Si potrebbe anche verificare alcune delle opzioni in Comparing E-mail Address Validating Regular Expressions (l'espressione regolare attualmente utilizzata in PHP è una di quelle testate).

Quindi è possibile scegliere una regexp che ti piace di più e utilizzarla in una chiamata a preg_match().

Oppure è possibile prendere l'espressione regolare e sostituire quello nel file PHP/ext/filter/logical_filter.c, funzione php_filter_validate_email() e ricostruire PHP.

+2

Questo articolo lo copre :) – Andy

+0

Il link al blog di Michael Rushton ora ti porta a una pagina vuota :( – Matteo

+0

@Matteo, puoi trovare lo stesso codice nella sorgente PHP oppure trovare il blog di Michael Rushton nella macchina del wayback: https : //web.archive.org/web/20150910045413/http: //squiloople.com/2009/12/20/email-address-validation –

5

[email protected] sembra funzionare bene: http://codepad.org/5HDgMW5i

Ma ho sicuramente visto persone che si lamentano è avuto problemi, anche su SO. Con ogni probabilità, ha problemi, ma lo sarà anche una soluzione regex. Le specifiche dell'indirizzo e-mail sono molto, molto complicate (RFC XXXX).

Ecco perché l'unica soluzione per verificare le email su cui dovresti fare affidamento è inviare un messaggio di posta elettronica all'indirizzo e richiedere un'azione (es .: se si tratta di uno script di registrazione, chiedere loro di fare clic su un link di verifica).

+0

Grazie per quello. Invierò un'email di convalida sarebbe bello avere qualcosa in atto per assicurarmi che le persone non inseriscano accidentalmente caratteri errati. – willdanceforfun

+3

@ Keen Non sto dicendo di buttare fuori dalla finestra la validazione regex; puoi validarlo e se fallisce avvisa l'utente (es: "il computer dice che la tua email non è valida, ma non è molto intelligente. sei sicuro di voler usare questa email?") – NullUserException

+0

Questa è una grande idea! – willdanceforfun

1
function isValidEmail($email, $checkDNS = false) 
{ 

    $valid = (
      /* Preference for native version of function */ 
      function_exists('filter_var') and filter_var($email, FILTER_VALIDATE_EMAIL) 
      ) || (
       /* The maximum length of an e-mail address is 320 octets, per RFC 2821. */ 
       strlen($email) <= 320 
       /* 
       * The regex below is based on a regex by Michael Rushton. 
       * However, it is not identical. I changed it to only consider routeable 
       * addresses as valid. Michael's regex considers [email protected] a valid address 
       * which conflicts with section 2.3.5 of RFC 5321 which states that: 
       * 
       * Only resolvable, fully-qualified domain names (FQDNs) are permitted 
       * when domain names are used in SMTP. In other words, names that can 
       * be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed 
       * in Section 5) are permitted, as are CNAME RRs whose targets can be 
       * resolved, in turn, to MX or address RRs. Local nicknames or 
       * unqualified names MUST NOT be used. 
       * 
       * This regex does not handle comments and folding whitespace. While 
       * this is technically valid in an email address, these parts aren't 
       * actually part of the address itself. 
       */ 
       and preg_match_all(
        '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?))'. 
        '{255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?))'. 
        '{65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|'. 
        '(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))'. 
        '(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|'. 
        '(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|'. 
        '(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})'. 
        '(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126})'.'{1,}'. 
        '(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|'. 
        '(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|'. 
        '(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::'. 
        '(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|'. 
        '(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|'. 
        '(?:(?!(?:.*[a-f0-9]:){5,})'.'(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::'. 
        '(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|'. 
        '(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|'. 
        '(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD', 
        $email) 
      ); 

    if($valid) 
    { 
     if($checkDNS && ($domain = end(explode('@',$email, 2)))) 
     { 
      /* 
      Note: 
      Adding the dot enforces the root. 
      The dot is sometimes necessary if you are searching for a fully qualified domain 
      which has the same name as a host on your local domain. 
      Of course the dot does not alter results that were OK anyway. 
      */ 
      return checkdnsrr($domain . '.', 'MX'); 
     } 
     return true; 
    } 
    return false; 
} 


//----------------------------------------------------------------- 

    var_dump(isValidEmail('[email protected]', true)); 
    // bool(true) 
+6

Si prega di non fornire semplicemente il codice come risposta. –

Problemi correlati