diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index bd54885d9d1b..a185deed9a20 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -179,9 +179,17 @@ public static function camel($value) */ public static function contains($haystack, $needles) { - foreach ((array) $needles as $needle) { - if ($needle !== '' && mb_strpos($haystack, $needle) !== false) { - return true; + if (version_compare(PHP_VERSION, '8.0', '>=')) { + foreach ((array) $needles as $needle) { + if (str_contains($haystack, $needle)) { + return true; + } + } + } else { + foreach ((array) $needles as $needle) { + if ($needle !== '' && mb_strpos($haystack, $needle) !== false) { + return true; + } } } @@ -215,9 +223,17 @@ public static function containsAll($haystack, array $needles) */ public static function endsWith($haystack, $needles) { - foreach ((array) $needles as $needle) { - if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) { - return true; + if (function_exists('str_ends_with')) { + foreach ((array) $needles as $needle) { + if (str_ends_with($haystack, $needle)) { + return true; + } + } + } else { + foreach ((array) $needles as $needle) { + if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) { + return true; + } } } @@ -652,9 +668,17 @@ public static function snake($value, $delimiter = '_') */ public static function startsWith($haystack, $needles) { - foreach ((array) $needles as $needle) { - if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) { - return true; + if (function_exists('str_starts_with')) { + foreach ((array) $needles as $needle) { + if (str_starts_with($haystack, $needle)) { + return true; + } + } + } else { + foreach ((array) $needles as $needle) { + if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) { + return true; + } } }