note 47599 added to function.fnmatch

From: Date: Mon, 22 Nov 2004 15:41:26 +0000
Subject: note 47599 added to function.fnmatch
Groups: php.notes 
Request: Send a blank email to php-notes+get-80876@lists.php.net to get a copy of this message
I've found a bug in the my_fnmatch() function of 'michael at zend dot com'. If you call : my_fnmatch('c:/tem?', 'c:/tempo'); It will match, this is an error since the '?' generic character is supposed to match only one character. The bug was hard to find since this is really an exception... Here is my corrected version, I think it's bug-free now :) function my_fnmatch ($pattern, $file) { $lenpattern = strlen($pattern); $lenfile = strlen($file); for($i=0 ; $i<$lenpattern ; $i++) { if($pattern[$i] == "*") { for($c=$i ; $c<max($lenpattern, $lenfile) ; $c++) { if(my_fnmatch(substr($pattern, $i+1), substr($file, $c))) return true; } return false; } if($pattern[$i] == "[") { $letter_set = array(); for($c=$i+1 ; $c<$lenpattern ; $c++) { if($pattern[$c] != "]") array_push($letter_set, $pattern[$c]); else break; } foreach($letter_set as $letter) { if(my_fnmatch($letter.substr($pattern, $c+1), substr($file, $i))) return true; } return false; } if($pattern[$i] == "?") continue; if($pattern[$i] != $file[$i]) return false; } if($pattern[$i - 1] == "?") return false; return true; } The bug correction is at the end of the function (the 'if' test). Cheers, Jean-Charles Lefebvre ---- Manual Page -- http://www.php.net/manual/en/function.fnmatch.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47599 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47599&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47599&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47599&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47599&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47599&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47599&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47599&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#80876) next »