From d6b743eddbd44a5cb2c714b3b6d1ba9d5a5b072c Mon Sep 17 00:00:00 2001 From: salehhashemi1992 <81674631+salehhashemi1992@users.noreply.github.com> Date: Thu, 2 Nov 2023 21:01:05 +0330 Subject: [PATCH] Allow defaulting end parameter to start value in findBetween --- framework/helpers/BaseStringHelper.php | 15 ++++++++++----- tests/framework/helpers/StringHelperTest.php | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/framework/helpers/BaseStringHelper.php b/framework/helpers/BaseStringHelper.php index 749f92f79cd..285fefa78ba 100644 --- a/framework/helpers/BaseStringHelper.php +++ b/framework/helpers/BaseStringHelper.php @@ -529,17 +529,22 @@ public static function mask($string, $start, $length, $mask = '*') { } /** - * Returns the portion of the string that lies between the first occurrence of the start string - * and the last occurrence of the end string after that. + * Returns the portion of the string that lies between the first occurrence of the `$start` string + * and the last occurrence of the `$end` string after that. * * @param string $string The input string. * @param string $start The string marking the start of the portion to extract. - * @param string $end The string marking the end of the portion to extract. + * @param string|null $end The string marking the end of the portion to extract. + * If the `$end` string is not provided, it defaults to the value of the `$start` string. * @return string|null The portion of the string between the first occurrence of - * start and the last occurrence of end, or null if either start or end cannot be found. + * `$start` and the last occurrence of `$end`, or null if either `$start` or `$end` cannot be found. */ - public static function findBetween($string, $start, $end) + public static function findBetween($string, $start, $end = null) { + if ($end === null) { + $end = $start; + } + $startPos = mb_strpos($string, $start); if ($startPos === false) { diff --git a/tests/framework/helpers/StringHelperTest.php b/tests/framework/helpers/StringHelperTest.php index 94efbf67137..6a267e597f5 100644 --- a/tests/framework/helpers/StringHelperTest.php +++ b/tests/framework/helpers/StringHelperTest.php @@ -532,6 +532,8 @@ public function dataProviderFindBetween() ['no delimiters here', 'start', 'end', null], // no start and end ['start only', 'start', 'end', null], // start found but no end ['end only', 'start', 'end', null], // end found but no start + ['a1a2a3a', 'a', 'a', '1a2a3'], // same start and end + ['a1a2a3a', 'a', null, '1a2a3'], // end is null ['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters ['من صالح هاشمی هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages ];