简体   繁体   English

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

[英]Is glob() the best way to get specific files with specific string, or extension?

Is the method correct?方法是否正确?

3 different data types: 3种不同的数据类型:

  • with 'txt' extension (*.txt)带有“txt”扩展名 (*.txt)
  • contain '_th' in the name extension 'jpg' (* _th.jpg)在扩展名 'jpg' 中包含 '_th' (* _th.jpg)
  • only files without extension 'txt' and in name '_th'只有没有扩展名'txt'和名称'_th'的文件
  $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');

The first pattern for glob is wrong. glob 的第一个模式是错误的。 This here * [._th].这里 * [._th]。 Jpg does not mean that before.jpg is not a "_th", It means that the last character before the point not be permitted '_'. jpg 并不表示 before.jpg 不是“_th”,而是表示该点之前的最后一个字符不允许有 '_'。 no 't' and no 'h'.没有“t”,也没有“h”。

Use array_diff() to get all jpg-files which not contain thumbs.使用 array_diff() 获取所有不包含拇指的 jpg 文件。

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

$jpgWithoutThumbs = array_diff(glob($dir.'/*.jpg'), $thumbs);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM