Forum Moderators: coopster
Note that FILTER_VALIDATE_EMAIL used in isolation is not enough for most (if not all) web based registration forms.
It will happily pronounce "yourname" as valid because presumably the "@localhost" is implied, so you still have to check that the domain portion of the address exists.
if(filter_has_var(INPUT_POST, 'email') && !empty($_POST['email'])) {
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$email_pieces = explode("@", $_POST['email']);
if(count($email_pieces) === 2) {
if(preg_match('/^[^\.]+(\.[^\.]+){1,2}$/', $email_pieces[1])) {
// Valid
}
}
}
}
[edited by: Readie at 1:26 pm (utc) on Apr 19, 2010]
array of 10,000
Array
(
[strpos] => 0.00766682624817
[substr] => 0.0116670131683
[preg_match] => 0.0124950408936
)
array of 100,000
Array
(
[strpos] => 0.0817799568176
[substr] => 0.120522975922
[preg_match] => 0.125612974167
)
array of 1,000,000
Array
(
[strpos] => 0.805890083313
[substr] => 1.19799995422
[preg_match] => 1.25615906715
)
However you could quite easily fix that by doing something like:
if(filter_has_var(INPUT_POST, 'email') && !empty($_POST['email'])) {
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$email_pieces = explode("@", $_POST['email']);
if(count($email_pieces) === 2) {
if(preg_match('/^[^\.]+(\.[^\.]+){1,2}$/', $email_pieces[1])) {
// Valid
}
}
}
}
Or much simpler:
if(filter_has_var(INPUT_POST, 'email')
&& !empty($_POST['email'])
&& strpos($_POST['email'],'@')>0
&& filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
) {
echo 'You should have a valid e-mail address, but I haven't tested it yet.';
}
else {
echo 'Either you should enter a stinking e-mail or there was an error in the
system checking. My guess is you need to enter a valid e-mail address, but I
have not tested yet to be sure.';
}
if(filter_has_var(INPUT_POST, 'email')
&& !empty($_POST['email'])
&& preg_match('#^[^@]+@[a-z0-9][a-z0-9\-]{1,62}(\.[a-z]{2,4}|[a-z]{2,3}\.[a-z]{2})$#',$_POST['email'])
&& filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
) {
echo 'You should have a valid e-mail address, but I haven't tested it yet.';
}
else {
echo 'Either you should enter a stinking e-mail or there was an error in the
system checking. My guess is you need to enter a valid e-mail address, but I
have not tested yet to be sure.';
}
/* If you're not checking the characters for validity (They can't be a \d in
the tld and they can only be {2,4} or {2,3} {2} characters AFAIK.) The first one
should be the most accurate, the following misses on some goofy entries, like
the last example, but \d will allow .12.12 in the TLD, so it's 6 of 1 and half
dozen of the other IMO. */
<?php
$email=array('@example.com','visitor@example.com','example','example.com','visitor@',
'visitor@exa_mple.com','visitor@e.com','visitor@example.co.uk','visitor@examp.co.uk',
'visitor@ex.co.uk3','visitor@1234.co.uke');
$cnt=count($email);
for($i=0;$i<$cnt;$i++) {
if(
!empty($email[$i])
&& strpos($email[$i],'@')>0
&& strpos($email[$i],'.')>=(strpos($email[$i],'@')+3)
// the +3 could be +4 if you don't want to allow for 2 chars before the .
&& strlen($email[$i])>=(strpos($email[$i],'.')+3)
&& strlen($email[$i])<=(strpos($email[$i],'.')+7)
&& filter_var($email[$i], FILTER_VALIDATE_EMAIL)
) {
echo $i.'.) ';
echo 'You should have a valid e-mail address... I’m testing it now.';
echo '<br>'.$email[$i].'<br><br>';
}
else {
echo $i.'.) ';
echo 'Your email address is broken!';
echo '<br>'.$email[$i].'<br><br>';
}
}
?>
’
' - I've never even seen ’ before so I'm not convinced that it's backwards compatible.
<!ENTITY rsquo CDATA "’" -- right single quotation mark, U+2019 ISOnum -->
<?php
$cnt=count($email);
for($i=0;$i<$cnt;$i++) {
if(
!empty($email[$i])
&& filter_var($email[$i], FILTER_VALIDATE_EMAIL)
) {
echo $i.'.) ';
echo 'You should have a valid e-mail address... I’m testing it now.';
echo '<br>'.$email[$i].'<br><br>';
}
else {
echo $i.'.) ';
echo 'Your email address is broken!';
echo '<br>'.$email[$i].'<br><br>';
}
}
?>
But, that will need to wait. It's almost 1am here, I should of gone to bed an hour ago :)
BTW: As far as the apostrophe goes, isn't the key we all know and love on the keyboard just a 'not smart' single quote?
<?php
$cur = gettimeofday();
$now = $cur['sec'] . $cur['usec'];
echo $now;
$email = array(
'@example.com',
'visitor@example.com',
'example',
'example.com',
'visitor@',
'visitor@exa_mple.com',
'visitor@e.com',
'visitor@exmaple.co.uk',
'visitor@ex.co.uk',
'visitor@ex.co.uk3',
'visitor@1234.co.uke',
'visitor@ex.co.uk'
);
$count = count($email);
for($i = 0; $i < $count; $i++) {
// Other stuff goes here
}
$curr = gettimeofday();
$noww = $curr['sec'] . $curr['usec'];
echo '<br>' . $noww . '<br>' . ($noww - $now);
?>
if(preg_match('/^[^@]+@[a-z0-9][a-z0-9\-]{1,62}(\.[a-z]{2,4}|\.[a-z]{2,3}\.[a-z]{2})$/i', $email[$i])) {
echo '<br><span style="color: #00ff00;">' . $email[$i] . '</span>';
} else {
echo '<br><span style="color: #ff0000;">' . $email[$i] . '</span>';
}
if(isset($email[$i]) && !empty($email[$i])) {
if(filter_var($email[$i], FILTER_VALIDATE_EMAIL, array('flags' => FILTER_NULL_ON_FAILURE))) {
$email_pieces = explode("@", $email[$i]);
if(count($email_pieces) === 2) {
if(preg_match('/^[a-z0-9][a-z0-9\-]{1,62}(\.[a-z]{2,4}|\.[a-z]{2,3}\.[a-z]{2})$/i', $email_pieces[1])) {
echo '<br><span style="color: #00ff00;">' . $email[$i] . '</span>';
} else {
echo '<br><span style="color: #ff0000;">' . $email[$i] . '</span>';
}
} else {
echo '<br><span style="color: #ff0000;">' . $email[$i] . '</span>';
}
} else {
echo '<br><span style="color: #ff0000;">' . $email[$i] . '</span>';
}
} else {
echo '<br><span style="color: #ff0000;">' . $email[$i] . '</span>';
}
if(!empty($email[$i]) && strpos($email[$i], '@') > 0 && strpos($email[$i], '.') >= (strpos($email[$i], '@') +3) && strlen($email[$i]) >= (strpos($email[$i], '.') +3) && strlen($email[$i]) <= (strpos($email[$i], '.') +7) && filter_var($email[$i], FILTER_VALIDATE_EMAIL)) {
echo '<br><span style="color: #00ff00;">' . $email[$i] . '</span>';
} else {
echo '<br><span style="color: #ff0000;">' . $email[$i] . '</span>';
}
if(isset($email[$i]) && !empty($email[$i])) {
if(filter_var($email[$i], FILTER_VALIDATE_EMAIL, array('flags' => FILTER_NULL_ON_FAILURE))) {
$email_pieces = explode("@", $email[$i]);
if(count($email_pieces) === 2) {
if(preg_match('/^[^\.]+(\.[^\.]+){1,2}$/', $email_pieces[1])) {
echo '<br><span style="color: #00ff00;">' . $email[$i] . '</span>';
} else {
echo '<br><span style="color: #ff0000;">' . $email[$i] . '</span>';
}
} else {
echo '<br><span style="color: #ff0000;">' . $email[$i] . '</span>';
}
} else {
echo '<br><span style="color: #ff0000;">' . $email[$i] . '</span>';
}
} else {
echo '<br><span style="color: #ff0000;">' . $email[$i] . '</span>';
}
@ tangor
Uh, actually more of a brain glitch I think
I discovered the preg_match functions to be faster than many of the string functions back in 2005 when PHP5 was breaking the scene.
preg_replace('/a/', 'b', $input); as opposed to str_replace('a', 'b', $input);) - the sheer number of functions we need to call to validate something like an E-mail address to the same degree of accuracy just out-weighs that of a singular preg function.