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 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数