Bug #52788 [NEW]: preg_match not matching last backslash '\'

From: Date: Tue, 07 Sep 2010 05:37:45 +0000
Subject: Bug #52788 [NEW]: preg_match not matching last backslash '\'
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-154130@lists.php.net to get a copy of this message
From:             
Operating system: ALL
PHP version:      5.3.3
Package:          *Regular Expressions
Bug Type:         Bug
Bug description:preg_match not matching last backslash '\'

Description:
------------
preg_match() does not match the last backslash at the end of a subject
string.

I'm using the pattern "#^[^0-9a-zA-Z]*$#" to filter out any characters from
user input other than alphanumerics.

The Problem I'm seeing is if there is a backslash \ as the last character
with valid characters before it, then preg_match() does not match it.

Example input:
\    = matched.
\\   = matched.
a\a  = matched.
&@$  = matched.
a\   = not matched.
0\   = not matched.
A\   = not matched.
abc\ = not matched.
etc...

Test script:
---------------
// Obviously taking into account here that backslash \ is an escape char.
// SO double the backslash.

$userInput = "a\\";

if (preg_match("#^[^0-9a-zA-Z]*$#", $userInput))
{
    echo "Invalid input";
}
else
{
    echo "Good to go!";
}

// If you negate the regular expression to "#^[0-9a-zA-Z]*$#" and change
the
// test statement to be: 
// if (!preg_match("#^[0-9a-zA-Z]*$#", $userInput)) {}
// it works as expected.

Expected result:
----------------
Expected result: Invalid input

Actual result:
--------------
Actual result: Good to go!

-- 
Edit bug report at http://bugs.php.net/bug.php?id=52788&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=52788&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=52788&r=trysnapshot53
Try a snapshot (trunk):              http://bugs.php.net/fix.php?id=52788&r=trysnapshottrunk
Fixed in SVN:                        http://bugs.php.net/fix.php?id=52788&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52788&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=52788&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=52788&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=52788&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=52788&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=52788&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=52788&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=52788&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=52788&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=52788&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=52788&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=52788&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=52788&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=52788&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=52788&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=52788&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=52788&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#154130) next »