From 576dd3d3cfbd4ef5239eb277c4d6bd315d7503bd Mon Sep 17 00:00:00 2001 From: Lupinity Labs Date: Mon, 5 Oct 2020 15:47:39 +0200 Subject: [PATCH] [8.x] increase performance of Str::before by over 60%. (#34642) * [8.x] increase performance of Str::before by over 50%. * [8.x] refactor code for readability and a further 10% speed gain. --- src/Illuminate/Support/Str.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 59158f4c6ee7..266c4b9562ca 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -107,7 +107,13 @@ public static function ascii($value, $language = 'en') */ public static function before($subject, $search) { - return $search === '' ? $subject : explode($search, $subject)[0]; + if ($search === '') { + return $subject; + } + + $result = strstr($subject, (string) $search, true); + + return $result === false ? $subject : $result; } /**