|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-01-27 15:50 UTC] gohanman at gmail dot com
Description:
------------
I'm trying to validate email addresses via regular expression. My regular expression fails if the first domain portion is longer than 18 characters. Overall string length does not seem relevant. Decreasing the length of the user portion or TLD does not make a 19+ character domain portion work.
Test script:
---------------
function is_email($str){
if(preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$/i", $str))
return True;
return False;
}
var_dump(is_email("user@123456789012345678.com"));
var_dump(is_email("user@1234567890123456789.com"));
Expected result:
----------------
bool(true);
bool(true);
Actual result:
--------------
bool(true);
bool(false);
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 17:00:01 2025 UTC |
This is not a PHP bug. Your regex is written somewhat inefficiently (and doesn't do what you expect it to) and thus seems to quickly hit the backtracking limit. Btw, I can't repro it on 5.3, only 5.2. What you actually wanted to write is /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i (not the \.). By the way, we encourage you to use filter_var with FILTER_VALIDATE_EMAIL instead of a custom regex. Custom solutions usually are much too restrictive and disallow many valid emails.