Find the needle in a haystack, where the needle consists of many needles

I have a function that takes a string (haystack) and an array of strings (needles) and returns true if at least one needle is a substring of the haystack. It did not take much time or effort to write it, but I am wondering if there is a PHP function there that already does this.

function strstr_array_needle($haystack, $arrayNeedles){ foreach($arrayNeedles as $needle){ if(strstr($haystack, $needle)) return true; } return false; } 
+6
source share
6 answers

just an offer ...

 function array_strpos($haystack, $needles) { foreach($needles as $needle) if(strpos($haystack, $needle) !== false) return true; return false; } 
+9
source

I think the closest function will be array_walk_recursive () , but this requires a callback. Therefore, using this is likely to be more complex than what you already have.

+1
source

I'm not quite sure what you want to do, but I think in_array() can help you do what you are looking for.

 $needleArray = array(1, 2, 3); // the values we want to get from $outputArray = array( ... ); // the values to search for foreach ($outputArray as $value) { if (in_array($value, $needleArray)) { // do what you want to do...the $value exists in $needleArray } } 
0
source

If you are just trying to determine which needles exist in a haystack, I suggest the array_intersect function.

PHP.net Documentation

 <?php $array1 = array("a" => "green", "red", "blue"); $array2 = array("b" => "green", "yellow", "red"); $result = array_intersect($array1, $array2); print_r($result); ?> The above example will output: Array ( [a] => green [0] => red ) 

Basically, this will result in an array that displays all the values ​​that appear in both arrays. In your case, your code returns true if any needle is found. The following code will do this using the array_intersect function, although if this is any simpler than Charles's answer is debatable.

 if(sizeof(array_intersect($hackstack, $arrayNeedles)) > 0) return true; else return false; 

Again, I'm not sure exactly what your code is trying to do except true if any needle exists. If you can provide some context for what you want to achieve, there may be a better way.

Hope this helps.

0
source

There is no single function that behaves like strstr_array_needle (the name is misleading, I expect it to return the substring $haystack ). There are other functions that can be used instead of a loop, but they have no advantages and require more time. For instance:

 # iterates over entire array, though stops checking once a match is found array_reduce($needles, function($found, $needle) use ($haystack) { return $found || (strpos($haystack, $needle) !== false); }, false); # iterates over entire array and checks each needle, even if one is already found (bool)array_filter($needles, function($needle) use ($haystack) { return strpos($haystack, $needle) !== false; }); 
0
source

Here is a tested and working function:

 <?php function strpos_array($haystack, $needles, $offset = 0) { if (is_array($needles)) { foreach ($needles as $needle) { $pos = strpos_array($haystack, $needle); if ($pos !== false) { return $pos; } } return false; } else { return strpos($haystack, $needles, $offset); } } 
0
source

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


All Articles