Bug #60903 [Nab]: preg_match + operator fails with string exceeding length 18

From: Date: Fri, 27 Jan 2012 20:56:11 +0000
Subject: Bug #60903 [Nab]: preg_match + operator fails with string exceeding length 18
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-166872@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=60903&edit=1

 ID:                 60903
 Updated by:         nikic@php.net
 Reported by:        gohanman at gmail dot com
 Summary:            preg_match + operator fails with string exceeding
                     length 18
 Status:             Not a bug
 Type:               Bug
-Package:            PCRE related
+Package:            Regexps related
 Operating System:   Ubuntu
 PHP Version:        5.3.9
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2012-01-27 20:54:10] felipe@php.net

Are you really using 5.3.9 with the built-in PCRE library?
Your test case works just fine here.

If it returns false, you can check preg_last_error() and adjust the INI configuration accordingly.
About crashing, it's known behavior from PCRE library, take a look at the PCRE documentation
for more information.

------------------------------------------------------------------------
[2012-01-27 15:50:53] 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);


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=60903&edit=1


Thread (3 messages)

« previous php.bugs (#166872) next »