【问题标题】:how to sort an array of files by keyword match of file name如何通过文件名的关键字匹配对文件数组进行排序
【发布时间】:2012-10-15 02:36:32
【问题描述】:

我有这个排序功能,它扫描一个目录并列出所有jpg文件,我怎样才能让它只对文件名与指定关键字匹配的jpg文件进行排序,例如查找和排序所有@987654323 @ 名称中包含关键字"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];  

  }

【问题讨论】:

    标签: php arrays sorting dir


    【解决方案1】:

    您可能希望使用 strpos() - http://php.net/manual/en/function.strrpos.php 检查文件名中出现的关键字,并且只将这些文件添加到您的数组中。假设您想按文件名的字母顺序排序,您可以使用 sort() 函数对该数组进行排序 - http://php.net/manual/en/function.sort.php

    使用strpos() 时,请务必测试!==false,否则文件名开头的关键字(例如“toys_picture.jpg”)将返回0,这是错误的但不是错误的。

    您还可以使用strrpos() - http://www.php.net/manual/en/function.strrpos.php - 查找文件名中最后出现的.,并将其用于substr(),如果您想支持 3 和 4 字符文件扩展名(均为 "jpg ”和“jpeg”)。

    $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;
    }
    

    【讨论】:

    • 非常感谢您的帮助,不幸的是,我对数组感到非常困惑,我只是不明白。但是我尝试将您的代码添加到我的代码中,它没有引发错误,但也没有返回任何结果。我为每个关键字设置了 13 张图片,因此一组图片的名称如下:toys.jpg、toysa.jpg、toysb.jpg、toysc.jpg,而另一组图片可能命名为 car.jpg、carsa.jpg、carb.jpg等...所以我需要找到所有包含关键字玩具的 jpg 文件。
    • 您是否尝试添加一些 echo 行来查看代码在哪里出现问题?
    • @KarenBemusOsh - 我的错,我有 if(false!==strpos($imgfile, $keyword)) 的参数,应该是 strpos($haystack, $needle)。请查看我的编辑。
    • 是的,我有一个回声,但它只是返回 false 并且什么也不显示。我想在这里粘贴我的新代码,但我不知道在哪里粘贴(是的,我完全无知,哈哈)
    • 好的,我尝试了您的编辑代码,但现在出现以下错误警告:sort() 期望参数 1 为数组,给定为空
    【解决方案2】:

    使用strrpos 检查子字符串是否存在于另一个字符串中

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

    查看该页面上的示例,您应该能够匹配 toys,如果它不存在,则将其从数组中排除

    【讨论】:

      【解决方案3】:

      也许我错过了一些东西,但在你说“是的,它是 JPG”之后,你会做这样的事情:

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

      【讨论】:

        猜你喜欢
        • 2016-01-24
        • 2019-04-12
        • 2017-08-14
        • 2016-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-15
        相关资源
        最近更新 更多