From 9f45f91f4a183baa530ecf12d9bfbd6f609f5e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Kov=C3=A1cs?= Date: Tue, 1 Dec 2020 15:27:34 +0100 Subject: [PATCH] Make use of new PHP8 string functions --- src/Illuminate/Support/Str.php | 42 ++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 9 deletions(-) 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; + } } }