PHP使用搜索字符串扫描目录
是否有任何方法可以搜索文件数组以匹配特定字符串 所以我有一个目录,里面有图像,我把它们放在一个变量中 像这样PHP使用搜索字符串扫描目录,php,Php,是否有任何方法可以搜索文件数组以匹配特定字符串 所以我有一个目录,里面有图像,我把它们放在一个变量中 像这样 $images = scandir('directory goes here'); 现在,我想在图像数组中搜索具有特定名称的图像。 例如,所有名称以“cars\u掀背车”开头的图像 我试过使用glob函数,但似乎我做得不够 有没有办法做到这一点? 提前谢谢 使用preg_grep搜索具有模式的数组: $images = scandir('directory goes here'); $
$images = scandir('directory goes here');
现在,我想在图像数组中搜索具有特定名称的图像。
例如,所有名称以“cars\u掀背车”开头的图像
我试过使用glob函数,但似乎我做得不够
有没有办法做到这一点?
提前谢谢 使用preg_grep搜索具有模式的数组:
$images = scandir('directory goes here');
$result = preg_grep ("/^cars_hatchback.*/", $images);
来源:使用preg\u grep搜索具有模式的数组:
$images = scandir('directory goes here');
$result = preg_grep ("/^cars_hatchback.*/", $images);
来源:使用函数
例如,如果您有如下文件夹结构:
folder
|----- autopass2.jpg
|----- autopass3.png
|----- auto_pass2.jpg
|----- otherfile.xml
现在只需使用带星号的glob
函数:
$result = glob('folder/autopass*');
结果是:
array
(
0 => 'folder/autopass2.jpg'
1 => 'folder/autopass3.png'
)
使用函数
例如,如果您有如下文件夹结构:
folder
|----- autopass2.jpg
|----- autopass3.png
|----- auto_pass2.jpg
|----- otherfile.xml
现在只需使用带星号的glob
函数:
$result = glob('folder/autopass*');
结果是:
array
(
0 => 'folder/autopass2.jpg'
1 => 'folder/autopass3.png'
)
可以使用星号作为通配符来查找以特定字符串开头的文件名:
scandir(“汽车”)强>
Array
(
[0] => .
[1] => ..
[2] => cars_convertible1.jpg
[3] => cars_hatchback1.jpg
[4] => cars_hatchback2.jpg
)
Array
(
[0] => cars/cars_hatchback1.jpg
[1] => cars/cars_hatchback2.jpg
)
glob('cars/cars_掀背车*)强>
Array
(
[0] => .
[1] => ..
[2] => cars_convertible1.jpg
[3] => cars_hatchback1.jpg
[4] => cars_hatchback2.jpg
)
Array
(
[0] => cars/cars_hatchback1.jpg
[1] => cars/cars_hatchback2.jpg
)
可以使用星号作为通配符来查找以特定字符串开头的文件名:
scandir(“汽车”)强>
Array
(
[0] => .
[1] => ..
[2] => cars_convertible1.jpg
[3] => cars_hatchback1.jpg
[4] => cars_hatchback2.jpg
)
Array
(
[0] => cars/cars_hatchback1.jpg
[1] => cars/cars_hatchback2.jpg
)
glob('cars/cars_掀背车*)强>
Array
(
[0] => .
[1] => ..
[2] => cars_convertible1.jpg
[3] => cars_hatchback1.jpg
[4] => cars_hatchback2.jpg
)
Array
(
[0] => cars/cars_hatchback1.jpg
[1] => cars/cars_hatchback2.jpg
)
看一看php的
glob()
函数:看一看php的glob()
函数:非常感谢这很好地解释了它,我只是像白痴一样使用glob函数。非常感谢这很好地解释了它,我只是像白痴一样使用glob函数。