PHP code example of chr15k / string

1. Go to this page and download the library: Download chr15k/string library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chr15k / string example snippets


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"

$slice = s('This is my name')->after('This is');

// ' my name'

$slice = s('App\Controllers\Controller')->afterLast('\\');

// 'Controller'

$string = s('Hello')->append(' there!');

// 'Hello there!'

$string = s('ü')->ascii();

// 'u'

$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'

$slice = s('This is my name')->before('my name');

// 'This is '

$slice = s('This is my name')->beforeLast('is');

// 'This '

$converted = s('foo_bar')->camel();

// fooBar

$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 = s('This is my name')->containsAll(['my', 'name']);

// true

$string = s('/foo/bar/baz')->dirname();

// '/foo/bar'

// Optionally pass directory levels as second argument:
$string = s('/foo/bar/baz')->dirname(2);

// '/foo'

$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

$result = s('Chris')->exactly('Chris');

// true

$result = s(' Chris')->exactly('Chris');

// false

$result = s('Chris')->exactly('chris');

// false

$collection = s('foo bar baz')->explode(' ');

// ['foo', 'bar', 'baz']

$adjusted = s('this/string')->finish('/');

// this/string/

$adjusted = s('this/string/')->finish('/');

// this/string/

$result = s('Chris')->isAscii();

// true

$result = s('ü')->isAscii();

// false

$result = s('  ')->trim()->isEmpty();

// true

$result = s('Chris')->trim()->isEmpty();

// false

$result = s('  ')->trim()->isNotEmpty();

// false

$result = s('Chris')->trim()->isNotEmpty();

// true

$converted = s('fooBar')->kebab();

// foo-bar

$length = s('Chris')->length();

// 5

$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 (...)

$result = s('CHRIS')->lower();

// 'chris'

$string = s('  Chris  ')->ltrim();

// 'Chris  '

$string = s('/Chris/')->ltrim('/');

// 'Chris/'

$result = s('foo bar')->match('/bar/');

// 'bar'

$result = s('foo bar')->match('/foo (.*)/');

// 'bar'

$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 = s('Chris')->possessive();

// Chris'

$possessive = s('David')->possessive();

// David's

$possessive = s('it')->possessive();

// its

$string = s('World')->prepend('Hello ');

// Hello World

$replaced = s('Hello World')->replace('World', 'Chris');

// Hello Chris

$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

$replaced = s('the quick brown fox jumps over the lazy dog')->replaceFirst('the', 'a');

// a quick brown fox jumps over the lazy dog

$replaced = s('the quick brown fox jumps over the lazy dog')->replaceLast('the', 'a');

// the quick brown fox jumps over a lazy dog

$string = s('  Chris  ')->rtrim();

// '  Chris'

$string = s('/Chris/')->rtrim('/');

// '/Chris'

$singular = s('cars')->singular();

// car

$singular = s('children')->singular();

// child

$slug = s('Hello World')->slug('-');

// hello-world

$converted = s('fooBar')->snake();

// foo_bar

$segments = s('one, two, three')->split('/[\s,]+/');

// collect(["one", "two", "three"])

$adjusted = s('this/string')->start('/');

// /this/string

$adjusted = s('/this/string')->start('/');

// /this/string

$result = s('This is my name')->startsWith('This');

// true

$converted = s('foo_bar')->studly();

// FooBar

$string = s('Hello World')->substr(6);

// World

$string = s('Hello World')->substr(6, 3);

// Wo

$converted = s('a nice title uses the correct case')->title();

// A Nice Title Uses The Correct Case

$string = s('  Chris  ')->trim();

// 'Chris'

$string = s('/Chris/')->trim('/');

// 'Chris'

$string = s('foo bar')->ucfirst();

// Foo bar

$adjusted = s('chris')->upper();

// CHRIS

$string = s('  ')->whenEmpty(function ($string) {
    return $string->trim()->prepend('Chris');
});

// 'Chris'

$string = s('Perfectly balanced, as all things should be.')->words(3, ' >>>');

// Perfectly balanced, as >>>

$slice = Str::after('This is my name', 'This is');

// ' my name'

$slice = Str::afterLast('App\Controllers\Controller', '\\');

// 'Controller'

$slice = Str::before('This is my name', 'my name');

// 'This is '

$converted = Str::camel('foo_bar')

// fooBar

$contains = Str::contains('This is my name', 'my');

// true

$containsAll = Str::containsAll('This is my name', ['my', 'name']);

// true

$result = Str::endsWith('This is my name', 'name');

// true

$adjusted = Str::finish('this/string', '/');

// this/string/

$adjusted = Str::finish('this/string/', '/');

// this/string/

$isAscii = Str::isAscii('Chris');

// true

$isAscii = Str::isAscii('ü');

// false

$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');

// true

$isUuid = Str::isUuid('chris');

// false

$converted = Str::kebab('fooBar');

// foo-bar

$length = Str::length('Chris');

// 5

$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);

// The quick brown fox...

$lower = Str::lower('CHRIS');

// chris

$matches = Str::match('foo*', 'foobar');

// true

$matches = Str::match('baz*', 'foobar');

// false

$orderedUuid = Str::orderedUuid();

// 90f81d6c-b4f6-4b03-a82d-800058a21705

$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

$possessive = Str::possessive('Chris');

// Chris'

$possessive = Str::possessive('David');

// David's

$possessive = Str::possessive('it');

// its

$random = Str::random(40);

// odkX5tWGo3tb8hlNgdoVPjHxZR8xRzii1uFT1cxa

$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

$replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');

// a quick brown fox jumps over the lazy dog

$replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');

// the quick brown fox jumps over a lazy dog

$singular = Str::singular('cars');

// car

$singular = Str::singular('children');

// child

$slug = Str::slug('Chris The Coder', '-');

// chris-the-coder

$converted = Str::snake('fooBar');

// foo_bar

$adjusted = Str::start('this/string', '/');

// /this/string

$adjusted = Str::start('/this/string', '/');

// /this/string

$result = Str::startsWith('This is my name', 'This');

// true

$converted = Str::studly('foo_bar');

// FooBar

$converted = Str::title('a nice title uses the correct case');

// A Nice Title Uses The Correct Case

$string = Str::ucfirst('foo bar');

// Foo bar

$string = Str::upper('chris');

// CHRIS

$uuid = Str::uuid();

// 0b1a9d6f-e2c7-489d-93f9-331108ebc314

return Str::words('Perfectly balanced, as all things should be.', 3, ' >>>');

// Perfectly balanced, as >>>