简体   繁体   中英

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

Is the method correct?

3 different data types:

  • with 'txt' extension (*.txt)
  • contain '_th' in the name extension 'jpg' (* _th.jpg)
  • only files without extension 'txt' and in name '_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. This here * [._th]. Jpg does not mean that before.jpg is not a "_th", It means that the last character before the point not be permitted '_'. no 't' and no 'h'.

Use array_diff() to get all jpg-files which not contain thumbs.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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