[英]PHP to capitalize all letters (including after a slash) except for certain words
[英]How to use PHP to get all the image filenames except containing certain words?
我喜歡使用PHP獲取目錄中的所有文件名,除非文件名包含某個單詞,例如“縮略圖”。
這就是我獲取所有圖像文件名的方式:
$images = glob($imagesDir . $albumName . '/' . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
所以我嘗試了:
$images = glob($imagesDir . $albumName . '/' . '^(?!thumbnail$).*.{jpg,jpeg,png,gif}', GLOB_BRACE);
顯然它不會返回任何東西。 我不熟悉PHP中的正則表達式。 我做錯什么了嗎?
使用preg_grep()
您可以使用正則表達式排除包含“縮略圖”的文件。
$images = array_values(preg_grep('/^(?!.*thumbnail).*$/', glob($imagesDir . $albumName . '/' . '*.{jpg,jpeg,png,gif}', GLOB_BRACE)));
僅匹配不包含單詞的字符串的正則表達式可能會有些困難 ,而且通常效率也很低。 將regex和PHP結合使用可能是您的最佳選擇。 這樣的事情應該做:
$images = glob($imagesDir . $albumName . '/' . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$images = array_filter($images, function($s) { return strpos($s,'thumbnail') === false; });
另外,我最誠摯的歉意使我的答卷的第一部分超出了預期。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.