laravel-str maintained by boomdraw
Laravel Str Helper
A package that extends \Illuminate\Support\Str with additional string helpers
Installation
Via Composer
composer require boomdraw/laravel-str
Laravel
The package will automatically register itself.
Lumen
Register StrServiceProvider
// bootstrap/app.php:
$app->register(Boomdraw\Str\StrServiceProvider::class);
Usage and methods
use Illuminate\Support\Str;
between
The function returns the portion of the string specified by the start and end parameters.
Str::between(string $string, string $start, string $end, bool $strict = true);
The function returns string between $start and $end.
Str::between('foo bar baz', 'foo ', ' baz') === 'bar';
The function returns false if $strict = true and $start or $end is missing in the $string.
Str::between('foo bar baz', 'test ', ' test') === false;
Str::between('foo bar baz', 'foo ', ' test') === false;
Str::between('foo bar baz', 'test ', ' baz') === false;
For the disabled strict mode result will be:
- source
$stringif$startand$endare missing in the$string.
Str::between('foo bar baz', 'test ', ' test', false) === 'foo bar baz';
- source
$stringbefore$endif$startis missing in the$string.
Str::between('foo bar baz', 'test ', ' baz', false) === 'foo bar';
- source
$stringafter$startif$endis missing in the$string.
Str::between('foo bar baz', 'foo ', ' test', false) === 'bar baz';
wbetween
The function returns the portion of the string specified by the start and end parameters wrapped with those parameters.
Str::wbetween(string $string, string $start, string $end, bool $strict = true);
The function returns string between $start and $end wrapped with $start and $end.
Str::wbetween('goo foo bar baz haz', 'foo ', ' baz') === 'foo bar baz';
The function returns false if $strict = true and $start or $end is missing in the $string.
Str::wbetween('goo foo bar baz haz', 'test ', ' test') === false;
Str::wbetween('goo foo bar baz haz', 'foo ', ' test') === false;
Str::wbetween('goo foo bar baz haz', 'test ', ' baz') === false;
For the disabled strict mode result will be:
- source
$stringif$startand$endare missing in the$string.
Str::wbetween('goo foo bar baz haz', 'test ', ' test', false) === 'goo foo bar baz haz';
- source
$stringwithout substring after$endif$startis missing in the$string.
Str::wbetween('goo foo bar baz haz', 'test ', ' baz', false) === 'goo foo bar baz';
- source
$stringwithout substring before$startif$endis missing in the$string.
Str::wbetween('goo foo bar baz haz', 'foo ', ' test', false) === 'foo bar baz haz';
utrim
The function calls trim() function with slash (/) and backslash (\) added to charlist.
Str::utrim(string $string): string;
Str::utrim("\hello world \n") === 'hello world';
Testing
You can run the tests with:
composer test
Contributing
Please see CONTRIBUTING for details and a todo list.
Security
If you discover any security-related issues, please email pkgsecurity@boomdraw.com instead of using the issue tracker.