Uncaught Error: Call to undefined function str_starts_with() Fix Solutions
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle, $case = true)
{
if ($case) {
return strpos($haystack, $needle, 0) === 0;
}
return stripos($haystack, $needle, 0) === 0;
}
}
Example with the empty string
<?php
if (str_starts_with('abc', '')) {
echo "All strings start with the empty string";
}
?>
Example case-sensitivity
- Case Sensitive is a case where uppercase and lowercase letters are interpreted differently.
- Case Insensitive is a case where uppercase and lowercase letters are interpreted the same.
<?php
$string = 'The lazy fox jumped over the fence';
if (str_starts_with($string, 'The')) {
echo "The string starts with 'The'\n";
}
if (str_starts_with($string, 'the')) {
echo 'The string starts with "the"';
} else {
echo '"the" was not found because the case does not match';
}
?>
Uncaught Error: Call to undefined function str_ends_with() Fix Solutions
if (!function_exists('str_ends_with')) {
function str_ends_with($haystack, $needle, $case = true)
{
$expectedPosition = strlen($haystack) - strlen($needle);
if ($case) {
return strrpos($haystack, $needle, 0) === $expectedPosition;
}
return strripos($haystack, $needle, 0) === $expectedPosition;
}
}
Created at 2021-04-27 12:26:00
Updated at 2024-02-20 06:07:05
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.