|
|
Joined: Dec 1999
Posts: 30,867
Master Elite Member
|
Str_Glob_NoCase() - Creating a case-insensitive glob array for use in glob()This function is part of a solution when you want to use the PHP glob() function to return a list of files with a certain suffix, but you want the glob to perform matching in a case-insensitive manner. Originally, I wrote a function that returns this type of pattern: Input (string array): ["jpg","png"] Output: "{[jJ][pP][gG],[pP][nN][gG]}" On Linux, this works fine, but on Windows, the glob() function (using the GLOB_BRACE flag) will not handle this correctly (see PHP bug 69628). As a result, we have to take this new approach. Be warned, however, that PHP limits the glob expression to 260 characters, so trying to glob more than a few file extensions will quickly exhaust this limitation.
function Str_Glob_NoCase ($Suffix) {
$r = array();
if (strlen($Suffix) === 0) return $r;
$Mask = str_repeat('0', strlen($Suffix));
$Suffix = strtolower($Suffix);
$r[] = $Suffix;
while (true) {
$x = base_convert($Mask, 2, 10);
$x++;
$Mask = str_pad(base_convert($x, 10, 2), strlen($Mask), "0", STR_PAD_LEFT);
$xSuffix = $Suffix;
for ($i = 0; $i < strlen($Mask); $i++) {
if ($Mask[$i] == '1') $xSuffix[$i] = strtoupper($xSuffix[$i]);
}
$r[] = $xSuffix;
if (strpos($Mask, "0") === false) break;
}
return $r;
}
|
|
|
|
|