Created
December 2, 2016 09:30
-
-
Save Mattamorphic/b19083f57706ba1f40d576ce0161f4b6 to your computer and use it in GitHub Desktop.
Multi-stripos with Array of Needles in Haystack and Optional Filtering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Perform an STRIPOS with multiple needles, and optionally filter the result to a bool | |
* @param string $haystack The string to search | |
* @param array $needles The array of needles to hunt for | |
* @param int $offset The offset to start at | |
* @param bool $filter Should we filter the result to a boolean? | |
* | |
* @return mixed | |
**/ | |
function multi_stripos(string $haystack, array $needles, int $offset = 0, bool $filter = false) | |
{ | |
$found = array_flip($needles); | |
array_walk( | |
$found, | |
function(&$needle, $key) use ($haystack, $offset) { | |
$needle = stripos($haystack, $key, $offset); | |
} | |
); | |
return ($filter) ? count(array_filter($found, function($item) { return $item !== false; })) > 0 : $found; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment