doudi4014 2012-10-24 23:55
浏览 86

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

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];  

  }
  • 写回答

3条回答 默认 最新

  • dongmin4990 2012-10-25 00:00
    关注

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败