/    Sign up×
Community /Pin to ProfileBookmark

Is PHP confused about FILTER_VALIDATE_EMAIL?

HI

Please consider the following code:

[code=php]
var_dump(filter_var(‘[email protected]’, FILTER_VALIDATE_EMAIL));
[/code]

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: [url]http://codepad.org/MtDCoxxZ[/url]
Output: false

Site: [url]http://writecodeonline.com/php/[/url]
Output: True

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@denbrianAug 14.2013 — I need more details about you topics, can you send me?

______________________________________________
http://www.mediaidentity.co.uk/
Copy linkTweet thisAlerts:
@NogDogAug 14.2013 — 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:
[code=php]
<?php

$regex = '#(?:[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:.[a-z0-9!#$%&amp;'*+/=?^_{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|\[x01-x09x0bx0cx0e-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-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|\[x01-x09x0bx0cx0e-x7f])+)])#';

$test = preg_match($regex, "[email protected]");
var_dump($test); // (int) 1
[/code]
Copy linkTweet thisAlerts:
@The_AlchemistAug 18.2013 — Try using this :
[code=php]$pattern = '/[^pLpN!#$%&'*+-/=?^_`{|}~@.[]]/u';
$email = preg_replace($pattern, '', '[email protected]');[/code]


I've tested it.
Copy linkTweet thisAlerts:
@phantom007authorAug 19.2013 — Try using this :
[code=php]$pattern = '/[^pLpN!#$%&'*+-/=?^_`{|}~@.[]]/u';
$email = preg_replace($pattern, '', '[email protected]');[/code]


I've tested it.[/QUOTE]



Thanks but what will it do?
Copy linkTweet thisAlerts:
@The_AlchemistAug 19.2013 — You said you're having issues with filter_var() so, here's something that'll do the work.
Copy linkTweet thisAlerts:
@NogDogAug 19.2013 — 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.
×

Success!

Help @phantom007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...