Can PHP's Glob() Be Made to Find Files in a Case Insensitive Manner

Can PHP's glob() be made to find files in a case insensitive manner?

Glob patterns support character ranges:

glob('my/dir/*.[cC][sS][vV]')

Search for files in a case insensitive manner?

$split=str_split($_GET['q']);//split each letter

$query='';
foreach($split as $b){ //loop
if(ctype_alpha($b)){//check its a letter
$query.='['.strtoupper($b).strtolower($b).']';//build upercase\lower case pair
}else{//if not a letter use raw version
$query.=$b;
}
}

then as you already have ..

foreach (glob("{$query}") as $filename) {

PHP glob with case-insensitive matching

May I suggest the build of case-insensitive character ranges on each letter of $str?

Code: (Demo)

function glob_i($string){  // this function is not multi-byte ready.
$result=''; // init the output string to allow concatenation
for($i=0,$len=strlen($string); $i<$len; ++$i){ // loop each character
if(ctype_alpha($string[$i])){ // check if it is a letter
$result.='['.lcfirst($string[$i]).ucfirst($string[$i]).']'; // add 2-character pattern
}else{
$result.=$string[$i]; // add non-letter character
}
}
return $result; // return the prepared string
}
$dir='public_html';
$str='Test Folder';

echo glob_i($str); // [tT][eE][sS][tT] [fF][oO][lL][dD][eE][rR]
echo "\n";
echo "$dir/*",glob_i($str),'*'; // public_html/*[tT][eE][sS][tT] [fF][oO][lL][dD][eE][rR]*

If you require a multibyte version, this is my suggested snippet: (Demo)

function glob_im($string,$encoding='utf8'){
$result='';
for($i=0,$len=mb_strlen($string); $i<$len; ++$i){
$l=mb_strtolower(mb_substr($string,$i,1,$encoding));
$u=mb_strtoupper(mb_substr($string,$i,1,$encoding));
if($l!=$u){
$result.="[{$l}{$u}]";
}else{
$result.=mb_substr($string,$i,1,$encoding);
}
}
return $result;
}
$dir='public_html';
$str='testovací složku';

echo glob_im($str); // [tT][eE][sS][tT][oO][vV][aA][cC][íÍ] [sS][lL][oO][žŽ][kK][uU]
echo "\n";
echo "$dir/*",glob_im($str),'*'; // public_html/*[tT][eE][sS][tT][oO][vV][aA][cC][íÍ] [sS][lL][oO][žŽ][kK][uU]*

Related Stackoverflow page:

Can PHP's glob() be made to find files in a case insensitive manner?


p.s. If you don't mind the expense of regex and/or you prefer a condensed one-liner, this will do the same: (Demo)

$dir='public_html';
$str='Test Folder';
echo "$dir/*",preg_replace_callback('/[a-z]/i',function($m){return '['.lcfirst($m[0]).ucfirst($m[0])."]";},$str),'*'; // $public_html/*[tT][eE][sS][tT] [fF][oO][lL][dD][eE][rR]*

and here is the multi-byte version: (Demo)

$encoding='utf8';
$dir='public_html';
$str='testovací složku';
echo "$dir/*",preg_replace_callback('/\pL/iu',function($m)use($encoding){return '['.mb_strtolower($m[0],$encoding).mb_strtoupper($m[0],$encoding)."]";},$str),'*'; // public_html/*[tT][eE][sS][tT][oO][vV][aA][cC][íÍ] [sS][lL][oO][žŽ][kK][uU]*

PHP - Case insensitive File Search

I ultimately ended up fetching all images from the folder and checking for each sku in the image name array.

The following code solved my problem:

$path = $image_path ."/*.{jpg,png,gif}";
$all_images = glob($path, GLOB_BRACE);
$icount = count($all_images);
for($i = 0; $i < $icount; $i++)
{
$all_images[$i] = str_replace($image_path.'/', '', $all_images[$i]);
}

foreach($products as $product){
$matches = preg_grep ('/^'.$product['sku'].'(\w+)/i', $all_images);
}

Nevertheless, I would love to see case-insensitive glob implemented in future.

How to get image files from a directory and order by last modified?

$files = glob("*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}",GLOB_BRACE);
$sorted_files=array(); /* a new array that have modification time as values
and files as keys the purpose is to sort files according to the values in reverse order */
foreach ($files as $file)
{
$sorted_files[$file]=filemtime($file);
}
arsort($sorted_files);
foreach ($sorted_files as $image=>$mtime)
{
echo basename($image)."<br />"; // show only image name if you want to show full path then use this code // echo $image."<br />";
echo '<img src="'.$image .'" alt="Random image" />'."<br /><br />";
}


Related Topics



Leave a reply



Submit