Skip to content

Commit 26032ad

Browse files
refactor findBetween method and add other tests
1 parent caa2029 commit 26032ad

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Yii Framework 2 Change Log
1414
- Enh #20030: Improve performance of handling `ErrorHandler::$memoryReserveSize` (antonshevelev, rob006)
1515
- Enh #20042: Add empty array check to `ActiveQueryTrait::findWith()` (renkas)
1616
- Enh #20032: Added `mask` method for string masking with multibyte support (salehhashemi1992)
17+
- Enh #20034: Added `findBetween` to retrieve a substring that lies between two strings (salehhashemi1992)
1718

1819

1920
2.0.49.2 October 12, 2023

framework/helpers/BaseStringHelper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,22 +535,23 @@ public static function mask($string, $start, $length, $mask = '*') {
535535
* @param string $string The input string.
536536
* @param string $start The string marking the start of the portion to extract.
537537
* @param string $end The string marking the end of the portion to extract.
538-
* @return string The portion of the string between the first occurrence of start and the last occurrence of end.
538+
* @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.
539540
*/
540541
public static function findBetween($string, $start, $end)
541542
{
542543
$startPos = mb_strpos($string, $start);
543544

544545
if ($startPos === false) {
545-
return '';
546+
return null;
546547
}
547548

548549
// Cut the string from the start position
549550
$subString = mb_substr($string, $startPos + mb_strlen($start));
550551
$endPos = mb_strrpos($subString, $end);
551552

552553
if ($endPos === false) {
553-
return '';
554+
return null;
554555
}
555556

556557
return mb_substr($subString, 0, $endPos);

tests/framework/helpers/StringHelperTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,18 @@ public function testFindBetween($string, $start, $end, $expectedResult)
522522
public function dataProviderFindBetween()
523523
{
524524
return [
525-
['hello world hello', 'hello ', ' world', ''], // end before start
526-
['This is a sample string', 'is ', ' string', 'is a sample'], // normal case
525+
['hello world hello', ' hello', ' world', null], // end before start
526+
['This is a sample string', ' is ', ' string', 'a sample'], // normal case
527527
['startendstart', 'start', 'end', ''], // end before start
528528
['startmiddleend', 'start', 'end', 'middle'], // normal case
529529
['startend', 'start', 'end', ''], // end immediately follows start
530530
['multiple start start end end', 'start ', ' end', 'start end'], // multiple starts and ends
531-
['', 'start', 'end', ''], // empty string
532-
['no delimiters here', 'start', 'end', ''], // no start and end
531+
['', 'start', 'end', null], // empty string
532+
['no delimiters here', 'start', 'end', null], // no start and end
533+
['start only', 'start', 'end', null], // start found but no end
534+
['end only', 'start', 'end', null], // end found but no start
535+
['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters
536+
['من صالح هاشمی هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages
533537
];
534538
}
535539
}

0 commit comments

Comments
 (0)