diff --git a/src/StringHelper.php b/src/StringHelper.php index 35e41a8..c9d841a 100644 --- a/src/StringHelper.php +++ b/src/StringHelper.php @@ -623,14 +623,20 @@ public static function rtrim(string|array $string, string $pattern = self::DEFAU * 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. * + * If the end string is not provided, it defaults to the value of the start string. + * * @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. * @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. */ - public static function findBetween(string $string, string $start, string $end): ?string + public static function findBetween(string $string, string $start, ?string $end = null): ?string { + if ($end === null) { + $end = $start; + } + $startPos = mb_strpos($string, $start); if ($startPos === false) { @@ -651,13 +657,19 @@ public static function findBetween(string $string, string $start, string $end): * Returns the portion of the string between the initial occurrence of the 'start' string * and the next occurrence of the 'end' string. * + * If the end string is not provided, it defaults to the value of the start string. + * * @param string $string The input string. * @param string $start The string marking the beginning of the segment to extract. - * @param string $end The string marking the termination of the segment. + * @param string|null $end The string marking the termination of the segment. * @return string|null Extracted segment, or null if 'start' or 'end' is not present. */ - public static function findBetweenFirst(string $string, string $start, string $end): ?string + public static function findBetweenFirst(string $string, string $start, ?string $end = null): ?string { + if ($end === null) { + $end = $start; + } + $startPos = mb_strpos($string, $start); if ($startPos === false) { @@ -678,13 +690,19 @@ public static function findBetweenFirst(string $string, string $start, string $e * Returns the portion of the string between the latest 'start' string * and the subsequent 'end' string. * + * If the end string is not provided, it defaults to the value of the start string. + * * @param string $string The input string. * @param string $start The string marking the beginning of the segment to extract. - * @param string $end The string marking the termination of the segment. + * @param string|null $end The string marking the termination of the segment. * @return string|null Extracted segment, or null if 'start' or 'end' is not present. */ - public static function findBetweenLast(string $string, string $start, string $end): ?string + public static function findBetweenLast(string $string, string $start, ?string $end = null): ?string { + if ($end === null) { + $end = $start; + } + $endPos = mb_strrpos($string, $end); if ($endPos === false) { diff --git a/tests/StringHelperTest.php b/tests/StringHelperTest.php index 99ca7de..7895078 100644 --- a/tests/StringHelperTest.php +++ b/tests/StringHelperTest.php @@ -767,7 +767,7 @@ public function testInvalidTrimPattern(): void /** * @dataProvider dataProviderFindBetween */ - public function testFindBetween(string $string, string $start, string $end, ?string $expectedResult): void + public function testFindBetween(string $string, string $start, ?string $end, ?string $expectedResult): void { $this->assertSame($expectedResult, StringHelper::findBetween($string, $start, $end)); } @@ -786,6 +786,7 @@ public function dataProviderFindBetween(): array ['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 ]; @@ -794,7 +795,7 @@ public function dataProviderFindBetween(): array /** * @dataProvider dataProviderFindBetweenFirst */ - public function testFindBetweenFirst(string $string, string $start, string $end, ?string $expectedResult): void + public function testFindBetweenFirst(string $string, string $start, ?string $end, ?string $expectedResult): void { $this->assertSame($expectedResult, StringHelper::findBetweenFirst($string, $start, $end)); } @@ -815,6 +816,7 @@ public function dataProviderFindBetweenFirst(): array ['start only', 'start', 'end', null], // start found but no end ['end only', 'start', 'end', null], // end found but no start ['a1a2a3a', 'a', 'a', '1'], // same start and end + ['a1a2a3a', 'a', null, '1'], // end is null ['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters ['من صالح هاشمی هستم هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages ]; @@ -823,7 +825,7 @@ public function dataProviderFindBetweenFirst(): array /** * @dataProvider dataProviderFindBetweenLast */ - public function testFindBetweenLast(string $string, string $start, string $end, ?string $expectedResult): void + public function testFindBetweenLast(string $string, string $start, ?string $end, ?string $expectedResult): void { $this->assertSame($expectedResult, StringHelper::findBetweenLast($string, $start, $end)); } @@ -844,6 +846,7 @@ public function dataProviderFindBetweenLast(): array ['start only', 'start', 'end', null], // start found but no end ['end only', 'start', 'end', null], // end found but no start ['a1a2a3a', 'a', 'a', '3'], // same start and end + ['a1a2a3a', 'a', null, '3'], // end is null ['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters ['من صالح هاشمی هستم هستم', 'من ', ' هستم', 'صالح هاشمی هستم'], // other languages ];