Skip to content

Commit 6804fbe

Browse files
authored
Merge pull request #20034 from salehhashemi1992/feature/string-helper-get-between-
Implement StringHelper::findBetween Method
2 parents fcd9e0a + 3d4e108 commit 6804fbe

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

framework/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Yii Framework 2 Change Log
1313
- Enh #12743: Added new methods `BaseActiveRecord::loadRelations()` and `BaseActiveRecord::loadRelationsFor()` to eager load related models for existing primary model instances (PowerGamer1)
1414
- Enh #20030: Improve performance of handling `ErrorHandler::$memoryReserveSize` (antonshevelev, rob006)
1515
- Enh #20042: Add empty array check to `ActiveQueryTrait::findWith()` (renkas)
16-
- Enh #20032: Added `mask` method for string masking with multibyte support (salehhashemi1992)
16+
- Enh #20032: Added `yii\helpers\BaseStringHelper::mask()` method for string masking with multibyte support (salehhashemi1992)
17+
- Enh #20034: Added `yii\helpers\BaseStringHelper::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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,4 +527,33 @@ public static function mask($string, $start, $length, $mask = '*') {
527527

528528
return $masked;
529529
}
530+
531+
/**
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.
534+
*
535+
* @param string $string The input string.
536+
* @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.
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.
540+
*/
541+
public static function findBetween($string, $start, $end)
542+
{
543+
$startPos = mb_strpos($string, $start);
544+
545+
if ($startPos === false) {
546+
return null;
547+
}
548+
549+
// Cut the string from the start position
550+
$subString = mb_substr($string, $startPos + mb_strlen($start));
551+
$endPos = mb_strrpos($subString, $end);
552+
553+
if ($endPos === false) {
554+
return null;
555+
}
556+
557+
return mb_substr($subString, 0, $endPos);
558+
}
530559
}

tests/framework/helpers/StringHelperTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,34 @@ public function testMask()
506506
$this->assertSame('em**[email protected]', StringHelper::mask('[email protected]', 2, 2));
507507
$this->assertSame('******email.com', StringHelper::mask('[email protected]', 0, 6));
508508
}
509+
510+
/**
511+
* @param string $string
512+
* @param string $start
513+
* @param string $end
514+
* @param string $expectedResult
515+
* @dataProvider dataProviderFindBetween
516+
*/
517+
public function testFindBetween($string, $start, $end, $expectedResult)
518+
{
519+
$this->assertSame($expectedResult, StringHelper::findBetween($string, $start, $end));
520+
}
521+
522+
public function dataProviderFindBetween()
523+
{
524+
return [
525+
['hello world hello', ' hello', ' world', null], // end before start
526+
['This is a sample string', ' is ', ' string', 'a sample'], // normal case
527+
['startendstart', 'start', 'end', ''], // end before start
528+
['startmiddleend', 'start', 'end', 'middle'], // normal case
529+
['startend', 'start', 'end', ''], // end immediately follows start
530+
['multiple start start end end', 'start ', ' end', 'start end'], // multiple starts and ends
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
537+
];
538+
}
509539
}

0 commit comments

Comments
 (0)