如何获取包含子字符串的所有文件

时间:2013-10-09 09:49:09

标签: php substring filenames

有人能帮助我吗?我有许多当前名称的照片,如:“black_abc”,“black_bcd”,“black_cde”,“white_abc”。如何只获取文件名包含“黑色”的文件?

3 个答案:

答案 0 :(得分:1)

glob将帮助您找到文件名中包含“Black”的所有文件:

$folder = "images";    //the folder containing all your images
$pattern = "*Black*";  //the word you are looking for

$files = glob($folder. '/' . $pattern, GLOB_BRACE);
foreach($files as $filename) {
  //Display all pictures
  echo "<img src='"$folder . "/" . $filename . "' />";
}

答案 1 :(得分:0)

strpos($filename, 'black') !== false

答案 2 :(得分:0)

像这样[利用 stripos() [ Case-Insensitive ]

<?php
$files=array("black_1","white_2","black_3");

for($i=0;$i<count($files);$i++)
{
if(stripos($files[i],'black'))
{
    echo "Filename is $files[$i]";
}
}