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

Implement findBetween(), findBetweenFirst(), and findBetweenLast() Methods #118

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## 2.3.1 October 30, 2023

- Enh #117: `WildcardPatters` uses memoization and accelerates ~2 times on repeated calls (@viktorprogger)
- Enh #118: Added yii\helpers\BaseStringHelper::findBetween() to retrieve a substring that lies between two strings (salehhashemi1992)

## 2.3.0 October 23, 2023

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Overall the helper has the following method groups.
- startsWithIgnoringCase
- endsWith
- endsWithIgnoringCase
- findBetween

### Truncation

Expand Down
28 changes: 28 additions & 0 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
*
* @return int The number of bytes in the given string.
*/
public static function byteLength(string|null $input): int

Check warning on line 52 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "PublicVisibility": --- Original +++ New @@ @@ * * @return int The number of bytes in the given string. */ - public static function byteLength(string|null $input) : int + protected static function byteLength(string|null $input) : int { return mb_strlen((string) $input, '8bit'); }
{
return mb_strlen((string)$input, '8bit');

Check warning on line 54 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MBString": --- Original +++ New @@ @@ */ public static function byteLength(string|null $input) : int { - return mb_strlen((string) $input, '8bit'); + return strlen((string) $input); } /** * Returns the portion of string specified by the start and length parameters.
}

/**
Expand Down Expand Up @@ -619,6 +619,34 @@
return preg_replace("#[$pattern]+$#uD", '', $string);
}

/**
* 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.
* @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
{
$startPos = mb_strpos($string, $start);

if ($startPos === false) {
return null;
}

$startPos += mb_strlen($start);
$endPos = mb_strrpos($string, $end, $startPos);

if ($endPos === false) {
return null;
}

return mb_substr($string, $startPos, $endPos - $startPos);
}

/**
* Ensure the input string is a valid UTF-8 string.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,4 +763,30 @@ public function testInvalidTrimPattern(): void

StringHelper::trim('string', "\xC3\x28");
}

/**
* @dataProvider dataProviderFindBetween
*/
public function testFindBetween(string $string, string $start, string $end, ?string $expectedResult): void
{
$this->assertSame($expectedResult, StringHelper::findBetween($string, $start, $end));
}

public function dataProviderFindBetween(): array
{
return [
['hello world hello', ' hello', ' world', null], // end before start
['This is a sample string', ' is ', ' string', 'a sample'], // normal case
['startendstart', 'start', 'end', ''], // end before start
['startmiddleend', 'start', 'end', 'middle'], // normal case
['startend', 'start', 'end', ''], // end immediately follows start
['multiple start start end end', 'start ', ' end', 'start end'], // multiple starts and ends
['', 'start', 'end', null], // empty string
['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
['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters
['من صالح هاشمی هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages
];
}
}
Loading