簡體   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