Skip to content

Commit 533b58d

Browse files
implement findBetween method in StringHelper
1 parent 13ad016 commit 533b58d

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/StringHelper.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,35 @@ public static function rtrim(string|array $string, string $pattern = self::DEFAU
619619
return preg_replace("#[$pattern]+$#uD", '', $string);
620620
}
621621

622+
/**
623+
* Returns the portion of the string that lies between the first occurrence of the start string
624+
* and the last occurrence of the end string after that.
625+
*
626+
* @param string $string The input string.
627+
* @param string $start The string marking the start of the portion to extract.
628+
* @param string $end The string marking the end of the portion to extract.
629+
* @return string|null The portion of the string between the first occurrence of
630+
* start and the last occurrence of end, or null if either start or end cannot be found.
631+
*/
632+
public static function findBetween(string $string, string $start, string $end): ?string
633+
{
634+
$startPos = mb_strpos($string, $start);
635+
636+
if ($startPos === false) {
637+
return null;
638+
}
639+
640+
// Cut the string from the start position
641+
$subString = mb_substr($string, $startPos + mb_strlen($start));
642+
$endPos = mb_strrpos($subString, $end);
643+
644+
if ($endPos === false) {
645+
return null;
646+
}
647+
648+
return mb_substr($subString, 0, $endPos);
649+
}
650+
622651
/**
623652
* Ensure the input string is a valid UTF-8 string.
624653
*

tests/StringHelperTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,4 +763,30 @@ public function testInvalidTrimPattern(): void
763763

764764
StringHelper::trim('string', "\xC3\x28");
765765
}
766+
767+
/**
768+
* @dataProvider dataProviderFindBetween
769+
*/
770+
public function testFindBetween(string $string, string $start, string $end, string $expectedResult): void
771+
{
772+
$this->assertSame($expectedResult, StringHelper::findBetween($string, $start, $end));
773+
}
774+
775+
public function dataProviderFindBetween(): array
776+
{
777+
return [
778+
['hello world hello', ' hello', ' world', null], // end before start
779+
['This is a sample string', ' is ', ' string', 'a sample'], // normal case
780+
['startendstart', 'start', 'end', ''], // end before start
781+
['startmiddleend', 'start', 'end', 'middle'], // normal case
782+
['startend', 'start', 'end', ''], // end immediately follows start
783+
['multiple start start end end', 'start ', ' end', 'start end'], // multiple starts and ends
784+
['', 'start', 'end', null], // empty string
785+
['no delimiters here', 'start', 'end', null], // no start and end
786+
['start only', 'start', 'end', null], // start found but no end
787+
['end only', 'start', 'end', null], // end found but no start
788+
['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters
789+
['من صالح هاشمی هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages
790+
];
791+
}
766792
}

0 commit comments

Comments
 (0)