Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make $end parameter nullable in findBetween Method #20062

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 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)
public static function findBetween($string, $start, $end = null)
{
if ($end === null) {
$end = $start;
}

$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
Loading