繁体   English   中英

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

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

方法是否正确?

3种不同的数据类型:

  • 带有“txt”扩展名 (*.txt)
  • 在扩展名 'jpg' 中包含 '_th' (* _th.jpg)
  • 只有没有扩展名'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');

glob 的第一个模式是错误的。 这里 * [._th]。 jpg 并不表示 before.jpg 不是“_th”,而是表示该点之前的最后一个字符不允许有 '_'。 没有“t”,也没有“h”。

使用 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