Search string with an array of values ​​using PHP

I am trying to find a list of files and only do work on files with names that contain multiple values ​​in the array. I hope that I don't have to go through the array every time I have a new file name.
ala -

$needle = array('blah', 'bleh'); foreach($file_list as $file) { foreach($needle as $need) if(strstr($file, $need) !== false) { // do something... } } 

I just need to know if one of the lines in the array is in the file name, not the location of the line.

I would like to use something similar to strstr() , but this does not allow using the array as a needle.

.- i.e.

 if(strstr($haystack, array('blah', 'bleh'))) { // do something... } 

I would prefer to stay away from regular expressions, it seems to be a sled for working with a hammer. any ideas?

+4
source share
6 answers

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)) { // do something } } 

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))) { // do something } } 

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 .

+6
source

Patrick

You can use in_array (). Example:

 foreach($file_list as $file){ if(in_array($file, $needle)){ //-------------- // Needle found } } 

You can find more examples here: http://php.net/manual/en/function.in-array.php

+1
source

This will result in action in all files containing all the needles inside their name.

 // Loop through files foreach($files as $file){ foreach($needles as $needle){ // if filename does not contain this needle if(strpos($file, $needle) === false) continue 2; // skip to next file } // this file matches criteria. do smth on this file } 
+1
source

in_array may work very well, however your exact needle array will require an exact match.

You can implode array with some hash string, and use the result with the strstr or strpos option - str returns the first occurrence, pos only the position of the first occurrence - Make must have a very unique string, with an unusual prefix and suffix. It is rather a quick and dirty approach, but it may just work in your situation.

EDIT I was just thinking, why don't you want to use a loop? This will be much faster than combining all the elements from an array. Your question is if there is a function to find partial matches using a reference array: it is not built-in, so you better use your own loop (or implement some function for this using a loop, of course)

0
source
 if( strstr( implode( " ", $array ), $search ) { //found } 

But a function with a loop and a return on detection consumes memory faster and less.

function searchArrayFast ($ array, $ search) {foreach ($ array as $ a) {if (strstr ($ a, $ search)) {return true; }} return false; }

0
source

Do not use preg_match () if you only want to check if one line is contained in another line. Use strpos () instead, as this will be faster. http://php.net/manual/en/function.preg-match.php

 $needle = array('blah', 'bleh'); foreach ($file_list as $file) { $result = $this->searchStrpos($file, $needle); if ($result !== false) { // do something... } } /** * * @param String $string String search * @param mixed $needle * @param Boolean $onlyFirst return true but * @return Boolean Return boolean for search */ private function searchStrpos($string, $needle, $onlyFirst = true) { if (is_array($needle)) { foreach ($needle as $item) { $result = strpos($string, $item); if ($result !== false && $onlyFirst) { break; } } } else { $result = strpos($string, $needle); } return $result; } 
0
source

Source: https://habr.com/ru/post/1333584/


All Articles