如何通过文件名的关键字匹配对文件数组进行排序

I have this sort function which scans a directory and lists all jpg files, how could I make it to sort only jpg files whose file-name match a specified keyword, for example to find and sort all jpg files whose name includes the keyword "toys".

   $a_img[] = array(); // return values
$keyword = "toys"; // your keyword
$allowed_types = array('jpg'); // list of filetypes you want to show  
 $dimg = opendir($imgdir);  
 while($imgfile = readdir($dimg)) {
     // check to see if filename contains keyword
    if(false!==strpos($keyword, $imgfile)){
       //check file extension
        $extension = strtolower(substr($imgfile, strrpos($imgfile, ".")+1));
        if (in_array($extension, $allowed_types)) {
        // add file to your array
        $a_img[] = $imgfile;
        }
    }   
}
// sort alphabetically by filename
sort($a_img);


   $totimg = count($a_img); // total image number  
   for($x=0; $x < $totimg; $x++)  
       { 

    $size = getimagesize($imgdir.'/'.$a_img[$x]);  
   // do whatever  

   echo $a_img[$x];  

  }

You would want to check the filename for occurrences of your keyword using strpos() - http://php.net/manual/en/function.strrpos.php - and only add those files to your array. Assuming you want to sort alphabetically by filename, you sort this array using the sort() function - http://php.net/manual/en/function.sort.php

When using strpos() make sure to test for !==false otherwise your keyword at the beginning of the filename (for example "toys_picture.jpg") will return 0, which is falsey but not false.

You can also use strrpos() - http://www.php.net/manual/en/function.strrpos.php - to find the last occurrence of a . in your filename and use that for substr() should you want to support 3 and 4 character file extensions (both "jpg" and "jpeg" for example).

$imgdir = "idximages"; // directory
$keyword = "toys"; // your keyword
$allowed_types = array('jpg'); // list of filetypes you want to show  
$a_img = array(); // return values
$dimg = opendir($imgdir);  
while($imgfile = readdir($dimg)) {
    // check to see if filename contains keyword
    if(false!==strpos($imgfile, $keyword)){
        //check file extension
        $extension = strtolower(substr($imgfile, strrpos($imgfile, ".")+1));
        if (in_array($extension, $allowed_types)) {
            // add file to your array
            $a_img[] = $imgfile;
        }
    }   
}
// sort alphabetically by filename
sort($a_img);

// iterate through filenames
foreach ($a_img as $file){
    $imagesize = getimagesize($imgdir.'/'.$file);
    print_r($imagesize);
    list($width, $height, $type, $attr) = $imagesize;
}

Use strrpos to check for a substring's existence in another string

http://php.net/manual/en/function.strrpos.php

Look at the examples on that page and you should be able to match toys and exclude that from your array if it's not there

Perhaps I've missed something but after you've said "yes, it's a JPG" you'd do something like:

if(strstr($imgfile, 'toys'))
{
  //carry on
}