| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             [2010-09-07 08:03 UTC] rasmus@php.net
 
-Status: Open
+Status: Bogus
  [2010-09-07 08:03 UTC] rasmus@php.net
  [2010-09-07 09:48 UTC] reevsey at gmail dot com
  | 
    |||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 09:00:01 2025 UTC | 
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!