douzen3516 2013-01-09 17:35
浏览 39
已采纳

使用多个条件在PHP中进行目录搜索

I've been trying to find a way to search a directory with multiple criteria in PHP with little luck.

I have a folder that contains multiple versions of the same file but in different languages and for different devices:

e.g.

 1. file_en_v980.jar

 2. file_fr_v980.jar

 3. file_en_v990.jar

 4. file_fr_v990.jar

I want to be able to search the directory for all English (en) .jar files for the v980.

I've tried this:

foreach(glob('./'.$installer_location.'/'.$brand_name.'/*en*.jar') as $filename){
    echo $filename."</br>";
}  

It works, but returns the french (fr) file too.

I've tried this:

foreach(glob('./'.$installer_location.'/'.$brand_name.'/*en,v980*.jar') as $filename){
    echo $filename."</br>";
} 

But it returns nothing so I'm assuming I've got the syntax wrong.

Any help would be greatly appreciated.

Addition:

Based on the suggestion from the comment below I tried this:

foreach(glob('./'.$installer_location.'/'.$brand_name.'/*{en,v980}*.jar',GLOB_BRACE) as $filename){
    echo $filename."</br>";
} 

This returned all the files that had either en, v980 or both. I just need to figure out if there is a way to say must contain both.

  • 写回答

1条回答 默认 最新

  • dqdl6469 2013-01-09 17:53
    关注

    PHP's glob() essentially uses the same wildcarding/matching engine as a unix shell. With your version, you're searching for files that contain en,v980, and it's not being treated as "alternation". http://www.tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm

    Try

    glob('..../*{en,v980}*.jar')
    

    instead (note the {})

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题