Jump to content
php.lv forumi

problēmas ar email validation


yeahz

Recommended Posts

Nevajag riteni gudrot no jauna...

http://php.net/manual/en/function.filter-var.php

 

/**
* Funkcija pārbauda e-pasta adreses korektumu
* @param string $mail
* @return bool True - ja valīda e-pasta adrese, savādāk false
*/
function validMail($mail){
return (bool)filter_var($mail, FILTER_VALIDATE_EMAIL);
}

Edited by briedis
Link to comment
Share on other sites

Es daru sekojoši vismaz.

if(!preg_match("/^[-0-9A-Z_\.]{1,50}@([-0-9A-Z_\.]+\.){1,50}([0-9A-Z]){2,4}$/i", $epasts)){
echo "Nekorekta ēpasta adrese";
}

Domājams ka šis ritenis ar der.

 

Viens no sūdīgākajiem e-pastu regexpiem kādi redzēti :)

$mails = array(
"...@inbox.lv",
"---@inbox.lv",
"....@....000",
"____@___.123",
"-@..11",
);
foreach($mails as $mail){
if(preg_match("/^[-0-9A-Z_\.]{1,50}@([-0-9A-Z_\.]+\.){1,50}([0-9A-Z]){2,4}$/i", $mail)){
	echo "Labs e-pasts: $mail<br/>";
}
}

Rezultātā:

Labs e-pasts: ...@inbox.lv

Labs e-pasts: ---@inbox.lv

Labs e-pasts: ....@....000

Labs e-pasts: ____@___.123

Labs e-pasts: -@..11

 

 

Varbūt tomēr labāk ir lietot php piedāvātos iebūvētos rīkus?

Link to comment
Share on other sites

Tīri intereses pēc paskatījos kāda ir ātrdarbība un arī cik pareizi strādā. Kemito piedāvāto izteiksmi gan nācās nomainīt jo patiešām Briedis norādītos e-pasta variantus apstrādāja nekorekti, izteiksmi paņēmu no php.net komentāriem funkcijai preg_match. Interesanti tas ka preg_match strādā ātrāk, kaut gan sākumā domāju ka būs otrādāk.

 

Tātad ātruma pārbaude:

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');

/**
* Simple function to replicate PHP 5 behaviour
*/
function microtime_float() {
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

function validMail($email){
       return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
}

function validMail2($email) {
return (bool)preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i', $email);
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Mail</title>
</head>

<body>
<?php
set_time_limit(0);
$c = 10000;
$email = "mails@google.lv";

$time_start = microtime_float();
for ($i = 0; $i < $c; $i++)
validMail($email);
$time_end = microtime_float();
echo '<p>Filter_var izpildes laiks: '.($time_end - $time_start).' sekundes.</p>';

$time_start = microtime_float();
for ($i = 0; $i < $c; $i++)
validMail2($email);
$time_end = microtime_float();
echo '<p>Preg_match izpildes laiks: '.($time_end - $time_start).' sekundes.</p>';
?>
</body>
</html>

 

Rezultāts:

 

Filter_var izpildes laiks: 0.12879109382629 sekundes.

Preg_match izpildes laiks: 0.023126125335693 sekundes.

 

 

Pie reizes āri pareizības pārbaude.

 

$mail_set = array(
"...@inbox.lv",
"---@inbox.lv",
"....@....000",
"____@___.123",
"-@..11      ",
);

echo('<pre>');
foreach ($mail_set as $email)
echo ($email . "     Filter_var - " . (validMail(trim($email)) ? "1" : "0") . "     preg_match - " . (validMail2(trim($email)) ? "1" : "0") . "<br>");
echo('</pre>');

 

Rezultāts:

 

...@inbox.lv     Filter_var - 0     preg_match - 0
---@inbox.lv     Filter_var - 1     preg_match - 0
....@....000     Filter_var - 0     preg_match - 0
____@___.123     Filter_var - 0     preg_match - 0
-@..11           Filter_var - 0     preg_match - 0

 

Domāju ka Filter_var pārbauda vēl kādas papildus lietas, tāpēc arī lēnāk strādā.

Link to comment
Share on other sites

Ja nu ļoti gribas regexp :)

http://www.iamcal.com/publish/articles/php/parsing_email/

http://www.w3.org/Protocols/rfc822/

 

$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom  = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$pair  = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$pair)*\\x5d";
$quoted_string  = "\\x22($qtext|$pair)*\\x22";
$sub_domain     = "($atom|$domain_literal)";
$word           = "($atom|$quoted_string)";
$domain         = "$sub_domain(\\x2e$sub_domain)*";
$local_part     = "$word(\\x2e$word)*";
$expression     = "/^$local_part\\x40$domain$/D";

return (bool)preg_match($expression, $var);

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...