IMO great to use regular expressions here:
$pattern = '/' . implode('|', array_map('preg_quote', $needle)) . '/i'; foreach($file_list as $file) { if(preg_match($pattern, $file)) {
Link: preg_quote
Here is a more “creative” way to do this (but it uses internal loops and most likely slower as you actually loop several times over the array):
function cstrstr($haystack, $needle) { return strstr($haystack, $needle) !== false; } foreach($file_list as $file) { if(array_sum(array_map('cstrstr', array_pad(array($file), count($needle), $file), $needle))) {
But the benefits with regex should be obvious: you only need to create a pattern once , while in a “funny” solution you always need to create an array of length count($needle) for each $file .
source share