Skip to content

Commit d6b743e

Browse files
Allow defaulting end parameter to start value in findBetween
1 parent aa9adc8 commit d6b743e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

framework/helpers/BaseStringHelper.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -529,17 +529,22 @@ public static function mask($string, $start, $length, $mask = '*') {
529529
}
530530

531531
/**
532-
* Returns the portion of the string that lies between the first occurrence of the start string
533-
* and the last occurrence of the end string after that.
532+
* Returns the portion of the string that lies between the first occurrence of the `$start` string
533+
* and the last occurrence of the `$end` string after that.
534534
*
535535
* @param string $string The input string.
536536
* @param string $start The string marking the start of the portion to extract.
537-
* @param string $end The string marking the end of the portion to extract.
537+
* @param string|null $end The string marking the end of the portion to extract.
538+
* If the `$end` string is not provided, it defaults to the value of the `$start` string.
538539
* @return string|null The portion of the string between the first occurrence of
539-
* start and the last occurrence of end, or null if either start or end cannot be found.
540+
* `$start` and the last occurrence of `$end`, or null if either `$start` or `$end` cannot be found.
540541
*/
541-
public static function findBetween($string, $start, $end)
542+
public static function findBetween($string, $start, $end = null)
542543
{
544+
if ($end === null) {
545+
$end = $start;
546+
}
547+
543548
$startPos = mb_strpos($string, $start);
544549

545550
if ($startPos === false) {

tests/framework/helpers/StringHelperTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ public function dataProviderFindBetween()
532532
['no delimiters here', 'start', 'end', null], // no start and end
533533
['start only', 'start', 'end', null], // start found but no end
534534
['end only', 'start', 'end', null], // end found but no start
535+
['a1a2a3a', 'a', 'a', '1a2a3'], // same start and end
536+
['a1a2a3a', 'a', null, '1a2a3'], // end is null
535537
['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters
536538
['من صالح هاشمی هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages
537539
];

0 commit comments

Comments
 (0)