Skip to content

Instantly share code, notes, and snippets.

@Mattamorphic
Created December 2, 2016 09:30
Show Gist options
  • Save Mattamorphic/b19083f57706ba1f40d576ce0161f4b6 to your computer and use it in GitHub Desktop.
Save Mattamorphic/b19083f57706ba1f40d576ce0161f4b6 to your computer and use it in GitHub Desktop.
Multi-stripos with Array of Needles in Haystack and Optional Filtering
<?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