Add case insensitive string helper functions.

This commit is contained in:
Tony Murray
2016-08-30 11:55:24 -05:00
parent 583411046f
commit 38901bfe83

View File

@ -1389,6 +1389,24 @@ function str_contains($haystack, $needles)
return false;
}
/**
* Determine if a given string contains a given substring.
* Case Insensitive.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function str_icontains($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ($needle != '' && stripos($haystack, $needle) !== false) {
return true;
}
}
return false;
}
/**
* Determine if a given string ends with a given substring.
*
@ -1406,6 +1424,24 @@ function ends_with($haystack, $needles)
return false;
}
/**
* Determine if a given string ends with a given substring.
* Case insensitive.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function iends_with($haystack, $needles)
{
$lower_haystack = strtolower($haystack);
foreach ((array)$needles as $needle) {
if (strtolower($needle) === substr($lower_haystack, -strlen($needle))) {
return true;
}
}
return false;
}
/**
* Determine if a given string starts with a given substring.
@ -1423,3 +1459,21 @@ function starts_with($haystack, $needles)
}
return false;
}
/**
* Determine if a given string starts with a given substring.
* Case insensitive.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function istarts_with($haystack, $needles)
{
foreach ((array)$needles as $needle) {
if ($needle != '' && stripos($haystack, $needle) === 0) {
return true;
}
}
return false;
}