dongzhansong5785 2013-07-11 15:22
浏览 18
已采纳

在PHP目录列表中列出某些扩展

Have the following to provide a dir list in an array

for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != ".") { // don't list hidden files
 echo "<option value=\"".$dirArray[$index]."\">".$dirArray[$index]."</option>";
 }

Is there any way i can modify the above code so that only .JPG and .PNG are displayed?

Thanks!

CP

  • 写回答

3条回答 默认 最新

  • doutuobao9736 2013-07-11 15:27
    关注

    You can use regular expression to match if file name ends with .jpg or .png

    for($index=0; $index < $indexCount; $index++) 
    {
        if(preg_match("/^.*\.(jpg|png)$/i", $dirArray[$index]) == 1) 
        {
          echo "<option value=\"".$dirArray[$index]."\">".$dirArray[$index]."</option>";
        }
    }
    

    /i at the end of regular expression is case insensitive flag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程