Skip to content

Commit

Permalink
Allow defaulting end parameter to start value in findBetween
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed Nov 2, 2023
1 parent aa9adc8 commit 8cefb7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 9 additions & 4 deletions framework/helpers/BaseStringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
if ($end === null) {
$end = $start;

Check warning on line 545 in framework/helpers/BaseStringHelper.php

View check run for this annotation

Codecov / codecov/patch

framework/helpers/BaseStringHelper.php#L544-L545

Added lines #L544 - L545 were not covered by tests
}

$startPos = mb_strpos($string, $start);

if ($startPos === false) {
Expand Down
2 changes: 2 additions & 0 deletions tests/framework/helpers/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
Expand Down

0 comments on commit 8cefb7c

Please sign in to comment.