glob()是获取具有特定字符串或扩展名的特定文件的最佳方法吗?

问题描述 投票:0回答:1

方法正确吗?

3种不同的数据类型:

  • 带有'txt'扩展名(* .txt)
  • 名称扩展名为'jpg'(* _th.jpg)中包含'_th'
  • 仅扩展名为'_th'且不带扩展名'txt'的文件
  $dir = "./content/media/artworks";

  // ignore jpg-pictures if the name '_th' appears. And files with 'txt' extension.
  $imgs = glob($dir.'/*{[!_th].jpg,*.[!txt]}', GLOB_BRACE);

  // Just take jpg images with the name '_th'
  $thumbs = glob($dir.'/*?_th.*');

  // Just take files with 'txt' extensions
  $txts = glob($dir.'/*?.txt');
php glob
1个回答
0
投票

第一个glob模式是错误的。这是这里* [!_th]。 Jpg并不意味着.jpg之前不是“ _th”。这意味着不允许在该点之前的最后一个字符'_',no't'和no'h'。

使用array_diff()获取所有不包含拇指的jpg文件。

$thumbs = glob($dir.'/*_th.jpg');

$jpgWithoutThumbs = array_diff(glob($dir.'/*.jpg'), $thumbs);
© www.soinside.com 2019 - 2024. All rights reserved.