chr15k / string
Awesome String helpers package
1.0.5
2020-08-04 16:16 UTC
Requires
- php: ^7.4
- doctrine/inflector: ~1.4|^2.0
- ramsey/uuid: ^4.0
- voku/portable-ascii: ^1.5
Requires (Dev)
- phpunit/phpunit: ^9.2
README
This package provides useful helpers for working with strings in PHP, including UUID and ASCII support.
Based on ...
- Laravel's string helper work (https://github.com/laravel/framework)
Install
You can install this package via composer:
composer require chr15k/string
Usage
return s('Child') ->plural() ->possessive() ->append(' Book'); // outputs: "Children's Book" return s('Child') ->plural(1); // (pass a count) outputs: "Child" return s(' hello_world') ->trim() ->camel(); // outputs: "helloWorld" return s('Hello World') ->studly(); // outputs: "HelloWorld" return s('Hello World') ->slug('-'); // outputs: "hello-world"
Docs
String Method Chaining
Chain multiple string operations together using the s() helper.
- after
- afterLast
- append
- ascii
- basename
- before
- beforeLast
- camel
- contains
- containsAll
- dirname
- endsWith
- exactly
- explode
- finish
- isAscii
- isEmpty
- isNotEmpty
- kebab
- length
- limit
- lower
- ltrim
- match
- plural
- possessive
- prepend
- replace
- replaceArray
- replaceFirst
- replaceLast
- rtrim
- singular
- slug
- snake
- split
- start
- startsWith
- studly
- substr
- title
- trim
- ucfirst
- upper
- whenEmpty
- words
after
$slice = s('This is my name')->after('This is'); // ' my name'
afterLast
$slice = s('App\Controllers\Controller')->afterLast('\\'); // 'Controller'
append
$string = s('Hello')->append(' there!'); // 'Hello there!'
ascii
Transliterate the string to an ASCII value:
$string = s('ü')->ascii(); // 'u'
basename
$string = s('/foo/bar/baz')->basename(); // 'baz' // If needed, you may provide an "extension" that will be removed from the trailing component: $string = s('/foo/bar/baz.jpg')->basename('.jpg'); // 'baz'
before
$slice = s('This is my name')->before('my name'); // 'This is '
beforeLast
$slice = s('This is my name')->beforeLast('is'); // 'This '
camel
$converted = s('foo_bar')->camel(); // fooBar
contains
$contains = s('This is my name')->contains('my'); // true // You can also pass an array: $contains = s('This is my name')->contains(['my', 'foo']); // true
containsAll
$containsAll = s('This is my name')->containsAll(['my', 'name']); // true
dirname
$string = s('/foo/bar/baz')->dirname(); // '/foo/bar' // Optionally pass directory levels as second argument: $string = s('/foo/bar/baz')->dirname(2); // '/foo'
endsWith
$result = s('This is my name')->endsWith('name'); // true // You can also pass an array $result = s('This is my name')->endsWith(['name', 'foo']); // true $result = s('This is my name')->endsWith(['this', 'foo']); // false
exactly
$result = s('Chris')->exactly('Chris'); // true $result = s(' Chris')->exactly('Chris'); // false $result = s('Chris')->exactly('chris'); // false
explode
$collection = s('foo bar baz')->explode(' '); // ['foo', 'bar', 'baz']
finish
$adjusted = s('this/string')->finish('/'); // this/string/ $adjusted = s('this/string/')->finish('/'); // this/string/
isAscii
$result = s('Chris')->isAscii(); // true $result = s('ü')->isAscii(); // false
isEmpty
$result = s(' ')->trim()->isEmpty(); // true $result = s('Chris')->trim()->isEmpty(); // false
isNotEmpty
$result = s(' ')->trim()->isNotEmpty(); // false $result = s('Chris')->trim()->isNotEmpty(); // true
kebab
$converted = s('fooBar')->kebab(); // foo-bar
length
$length = s('Chris')->length(); // 5
limit
$truncated = s('The quick brown fox jumps over the lazy dog')->limit(20); // The quick brown fox... // pass second argument to append something other than '...' $truncated = s('The quick brown fox jumps over the lazy dog')->limit(20, ' (...)'); // The quick brown fox (...)
lower
$result = s('CHRIS')->lower(); // 'chris'
ltrim
$string = s(' Chris ')->ltrim(); // 'Chris ' $string = s('/Chris/')->ltrim('/'); // 'Chris/'
match
$result = s('foo bar')->match('/bar/'); // 'bar' $result = s('foo bar')->match('/foo (.*)/'); // 'bar'
plural
$plural = s('car')->plural(); // cars $plural = s('child')->plural(); // children // Pass second argument as a count to determine singular or plural form of a string: $plural = s('child')->plural(2); // children $plural = s('child')->plural(1); // child
possessive
$possessive = s('Chris')->possessive(); // Chris' $possessive = s('David')->possessive(); // David's $possessive = s('it')->possessive(); // its
prepend
$string = s('World')->prepend('Hello '); // Hello World
replace
$replaced = s('Hello World')->replace('World', 'Chris'); // Hello Chris
replaceArray
$string = 'The event will take place between ? and ?'; $replaced = s($string)->replaceArray('?', ['8:30', '9:00']); // The event will take place between 8:30 and 9:00
replaceFirst
$replaced = s('the quick brown fox jumps over the lazy dog')->replaceFirst('the', 'a'); // a quick brown fox jumps over the lazy dog
replaceLast
$replaced = s('the quick brown fox jumps over the lazy dog')->replaceLast('the', 'a'); // the quick brown fox jumps over a lazy dog
rtrim
$string = s(' Chris ')->rtrim(); // ' Chris' $string = s('/Chris/')->rtrim('/'); // '/Chris'
singular
$singular = s('cars')->singular(); // car $singular = s('children')->singular(); // child
slug
$slug = s('Hello World')->slug('-'); // hello-world
snake
$converted = s('fooBar')->snake(); // foo_bar
split
$segments = s('one, two, three')->split('/[\s,]+/'); // collect(["one", "two", "three"])
start
$adjusted = s('this/string')->start('/'); // /this/string $adjusted = s('/this/string')->start('/'); // /this/string
startsWith
$result = s('This is my name')->startsWith('This'); // true
studly
$converted = s('foo_bar')->studly(); // FooBar
substr
$string = s('Hello World')->substr(6); // World $string = s('Hello World')->substr(6, 3); // Wo
title
$converted = s('a nice title uses the correct case')->title(); // A Nice Title Uses The Correct Case
trim
$string = s(' Chris ')->trim(); // 'Chris' $string = s('/Chris/')->trim('/'); // 'Chris'
ucfirst
$string = s('foo bar')->ucfirst(); // Foo bar
upper
$adjusted = s('chris')->upper(); // CHRIS
whenEmpty
The whenEmpty method invokes the given Closure if the string is empty:
$string = s(' ')->whenEmpty(function ($string) { return $string->trim()->prepend('Chris'); }); // 'Chris'
words
$string = s('Perfectly balanced, as all things should be.')->words(3, ' >>>'); // Perfectly balanced, as >>>
String Methods
- after
- afterLast
- before
- camel
- contains
- containsAll
- endsWith
- finish
- isAscii
- isUuid
- kebab
- length
- limit
- lower
- match
- orderedUuid
- plural
- possessive
- random
- replaceArray
- replaceFirst
- replaceLast
- singular
- slug
- snake
- start
- startsWith
- studly
- title
- ucfirst
- upper
- uuid
- words
Str::after()
$slice = Str::after('This is my name', 'This is'); // ' my name'
Str::afterLast()
$slice = Str::afterLast('App\Controllers\Controller', '\\'); // 'Controller'
Str::before()
$slice = Str::before('This is my name', 'my name'); // 'This is '
Str::camel()
$converted = Str::camel('foo_bar') // fooBar
Str::contains()
$contains = Str::contains('This is my name', 'my'); // true
Str::containsAll()
$containsAll = Str::containsAll('This is my name', ['my', 'name']); // true
Str::endsWith()
$result = Str::endsWith('This is my name', 'name'); // true
Str::finish()
$adjusted = Str::finish('this/string', '/'); // this/string/ $adjusted = Str::finish('this/string/', '/'); // this/string/
Str::isAscii()
$isAscii = Str::isAscii('Chris'); // true $isAscii = Str::isAscii('ü'); // false
Str::isUuid()
$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de'); // true $isUuid = Str::isUuid('chris'); // false
Str::kebab()
$converted = Str::kebab('fooBar'); // foo-bar
Str::length()
$length = Str::length('Chris'); // 5
Str::limit()
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20); // The quick brown fox...
Str::lower()
$lower = Str::lower('CHRIS'); // chris
Str::match()
$matches = Str::match('foo*', 'foobar'); // true $matches = Str::match('baz*', 'foobar'); // false
Str::orderedUuid()
The Str::orderedUuid() method generates a "timestamp first" UUID that may be efficiently stored in an indexed database column.
$orderedUuid = Str::orderedUuid(); // 90f81d6c-b4f6-4b03-a82d-800058a21705
Str::plural()
$plural = Str::plural('bus'); // buses $plural = Str::plural('child'); // children // Pass second argument to retrieve the singular or plural form of the string... $plural = Str::plural('child', 2); // children $plural = Str::plural('child', 1); // child
Str::possessive()
$possessive = Str::possessive('Chris'); // Chris' $possessive = Str::possessive('David'); // David's $possessive = Str::possessive('it'); // its
Str::random()
$random = Str::random(40); // odkX5tWGo3tb8hlNgdoVPjHxZR8xRzii1uFT1cxa
Str::replaceArray()
$string = 'The event will take place between ? and ?'; $replaced = Str::replaceArray('?', ['8:30', '9:00'], $string); // The event will take place between 8:30 and 9:00
Str::replaceFirst()
$replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog'); // a quick brown fox jumps over the lazy dog
Str::replaceLast()
$replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog'); // the quick brown fox jumps over a lazy dog
Str::singular()
$singular = Str::singular('cars'); // car $singular = Str::singular('children'); // child
Str::slug()
$slug = Str::slug('Chris The Coder', '-'); // chris-the-coder
Str::snake()
$converted = Str::snake('fooBar'); // foo_bar
Str::start()
$adjusted = Str::start('this/string', '/'); // /this/string $adjusted = Str::start('/this/string', '/'); // /this/string
Str::startsWith()
$result = Str::startsWith('This is my name', 'This'); // true
Str::studly()
$converted = Str::studly('foo_bar'); // FooBar
Str::title()
$converted = Str::title('a nice title uses the correct case'); // A Nice Title Uses The Correct Case
Str::ucfirst()
$string = Str::ucfirst('foo bar'); // Foo bar
Str::upper()
$string = Str::upper('chris'); // CHRIS
Str::uuid()
$uuid = Str::uuid(); // 0b1a9d6f-e2c7-489d-93f9-331108ebc314
Str::words()
return Str::words('Perfectly balanced, as all things should be.', 3, ' >>>'); // Perfectly balanced, as >>>
Testing
You can run the tests with:
vendor/bin/phpunit
License
The MIT License (MIT). Please see License File for more information.