Strings manipulation

Backyard includes a variety of powerful helpers for string manipulations. These helpers have been extracted from the Laravel framework.

Str::after()

The Str::after method returns everything after the given value in a string. The entire string will be returned if the value does not exist within the string:

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

// ' my name'

Str::afterLast()

The Str::afterLast method returns everything after the last occurrence of the given value in a string. The entire string will be returned if the value does not exist within the string:

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

// 'Controller'

Str::before()

The Str::before method returns everything before the given value in a string:

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

// 'This is '

Str::beforeLast()

The Str::beforeLast method returns everything before the last occurrence of the given value in a string:

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

// 'This '

Str::between()

The Str::between method returns the portion of a string between two values:

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

// ' is my '

Str::camel()

The Str::camel method converts the given string to camelCase:

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

// fooBar

Str::contains()

The Str::contains method determines if the given string contains the given value (case sensitive):

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

// true

You may also pass an array of values to determine if the given string contains any of the values:

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

// true

Str::endsWith()

The Str::endsWith method determines if the given string ends with the given value:

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

// true

You may also pass an array of values to determine if the given string ends with any of the given values:

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

// true

$result = Str::endsWith('This is my name', ['this', 'foo']);

// false

Str::finish()

The Str::finish method adds a single instance of the given value to a string if it does not already end with the value:

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

// this/string/

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

// this/string/

Str::is()

The Str::is method determines if a given string matches a given pattern. Asterisks may be used to indicate wildcards:

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

// true

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

// false

Str::kebab()

The Str::kebab method converts the given string to kebab-case:

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

// foo-bar

Str::length()

The Str::length method returns the length of the given string:

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

// 7

Str::limit()

The Str::limit method truncates the given string at the specified length:

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

// The quick brown fox...

You may also pass a third argument to change the string that will be appended to the end:

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

// The quick brown fox (...)

Str::lower()

The Str::lower method converts the given string to lowercase:

$converted = Str::lower('LARAVEL');

// laravel

Str::replaceArray()

The Str::replaceArray method replaces a given value in the string sequentially using an array:

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

The Str::replaceFirst method replaces the first occurrence of a given value in a string:

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

The Str::replaceLast method replaces the last occurrence of a given value in a string:

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

// the quick brown fox jumps over a lazy dog

Str::snake()

The Str::snake method converts the given string to snake_case:

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

// foo_bar

Str::start()

The Str::start method adds a single instance of the given value to a string if it does not already start with the value:

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

// /this/string

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

// /this/string

Str::startsWith()

The Str::startsWith method determines if the given string begins with the given value:

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

// true

Str::studly()

The Str::studly method converts the given string to StudlyCase:

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

// FooBar

Str::substr()

The Str::substr method returns the portion of string specified by the start and length parameters:

$converted = Str::substr('The Laravel Framework', 4, 7);

// Laravel

Str::title()

The Str::title method converts the given string to Title Case:

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

// A Nice Title Uses The Correct Case

Str::ucfirst()

The Str::ucfirst method returns the given string with the first character capitalized:

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

// Foo bar

Str::upper()

The Str::upper method converts the given string to uppercase:

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

// LARAVEL

Str::words()

The Str::words method limits the number of words in a string:

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

// Perfectly balanced, as >>>