note 47599 deleted from function.fnmatch by victor
| From: | victor@php.net | Date: | Fri, 19 Aug 2005 11:31:10 +0000 |
| Subject: | note 47599 deleted from function.fnmatch by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-93887@lists.php.net to get a copy of this message | ||
Note Submitter: jcl [atNOSPAM] jcl [dot] name
----
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