-
Notifications
You must be signed in to change notification settings - Fork 5
Helpers Strings
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:
use Illuminate\Support\Str;
$slice = Str::after('This is my name', 'This is');
// ' my name'
The Str::ascii
method will attempt to transliterate the string into an ASCII value:
use Illuminate\Support\Str;
$slice = Str::ascii('û');
// 'u'
The Str::before
method returns everything before the given value in a string:
use Illuminate\Support\Str;
$slice = Str::before('This is my name', 'my name');
// 'This is '
The Str::camel
method converts the given string to camelCase:
use Illuminate\Support\Str;
$converted = Str::camel('foo_bar');
// fooBar
The Str::contains
method determines if the given string contains the given value. This method is case sensitive:
use Illuminate\Support\Str;
$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 in the array:
use Illuminate\Support\Str;
$contains = Str::contains('This is my name', ['my', 'foo']);
// true
The Str::containsAll
method determines if the given string contains all of the values in a given array:
use Illuminate\Support\Str;
$containsAll = Str::containsAll('This is my name', ['my', 'name']);
// true
The Str::endsWith
method determines if the given string ends with the given value:
use Illuminate\Support\Str;
$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 values in the array:
use Illuminate\Support\Str;
$result = Str::endsWith('This is my name', ['name', 'foo']);
// true
$result = Str::endsWith('This is my name', ['this', 'foo']);
// false
The Str::finish
method adds a single instance of the given value to a string if it does not already end with that value:
use Illuminate\Support\Str;
$adjusted = Str::finish('this/string', '/');
// this/string/
$adjusted = Str::finish('this/string/', '/');
// this/string/
The Str::is
method determines if a given string matches a given pattern. Asterisks may be used as wildcard values:
use Illuminate\Support\Str;
$matches = Str::is('foo*', 'foobar');
// true
$matches = Str::is('baz*', 'foobar');
// false
The Str::kebab
method converts the given string to kebab-case:
use Illuminate\Support\Str;
$converted = Str::kebab('fooBar');
// foo-bar
The Str::length
method returns the length of the given string:
use Illuminate\Support\Str;
$length = Str::length('Nexus');
// 7
The Str::limit
method truncates the given string to the specified length:
use Illuminate\Support\Str;
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);
// The quick brown fox... You may pass a third argument to the method to change the string that will be appended to the end of the truncated string:
use Illuminate\Support\Str;
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');
// The quick brown fox (...)
The Str::lower
method converts the given string to lowercase:
use Illuminate\Support\Str;
$converted = Str::lower('Nexus');
// Nexus
The Str::orderedUuid
method generates a "timestamp first" UUID that may be efficiently stored in an indexed database column. Each UUID that is generated using this method will be sorted after UUIDs previously generated using the method:
use Illuminate\Support\Str;
return (string) Str::orderedUuid();
The Str::plural
method converts a singular word string to its plural form. This function currently only supports the English language:
use Illuminate\Support\Str;
$plural = Str::plural('car');
// cars
$plural = Str::plural('child');
// children
You may provide an integer as a second argument to the function to retrieve the singular or plural form of the string:
use Illuminate\Support\Str;
$plural = Str::plural('child', 2);
// children
$singular = Str::plural('child', 1);
// child
The Str::pluralStudly
method converts a singular word string formatted in studly caps case to its plural form. This function currently only supports the English language:
use Illuminate\Support\Str;
$plural = Str::pluralStudly('VerifiedHuman');
// VerifiedHumans
$plural = Str::pluralStudly('UserFeedback');
// UserFeedback
You may provide an integer as a second argument to the function to retrieve the singular or plural form of the string:
use Illuminate\Support\Str;
$plural = Str::pluralStudly('VerifiedHuman', 2);
// VerifiedHumans
$singular = Str::pluralStudly('VerifiedHuman', 1);
// VerifiedHuman
The Str::random
method generates a random string of the specified length. This function uses PHP's random_bytes function:
use Illuminate\Support\Str;
$random = Str::random(40);
The Str::replaceArray
method replaces a given value in the string sequentially using an array:
use Illuminate\Support\Str;
$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
The Str::replaceFirst
method replaces the first occurrence of a given value in a string:
use Illuminate\Support\Str;
$replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');
// a quick brown fox jumps over the lazy dog
The Str::replaceLast
method replaces the last occurrence of a given value in a string:
use Illuminate\Support\Str;
$replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');
// the quick brown fox jumps over a lazy dog
The Str::singular
method converts a string to its singular form. This function currently only supports the English language:
use Illuminate\Support\Str;
$singular = Str::singular('cars');
// car
$singular = Str::singular('children');
// child
The Str::slug
method generates a URL friendly "slug" from the given string:
use Illuminate\Support\Str;
$slug = Str::slug('Nexus 5 -', '-');
// Nexus-5--
The Str::snake
method converts the given string to snake_case:
use Illuminate\Support\Str;
$converted = Str::snake('fooBar');
// foo_bar
$converted = Str::snake('fooBar', '-');
// foo-bar
The Str::start
method adds a single instance of the given value to a string if it does not already start with that value:
use Illuminate\Support\Str;
$adjusted = Str::start('this/string', '/');
// /this/string
$adjusted = Str::start('/this/string', '/');
// /this/string
The Str::startsWith
method determines if the given string begins with the given value:
use Illuminate\Support\Str;
$result = Str::startsWith('This is my name', 'This');
// true
If an array of possible values is passed, the startsWith method will return true if the string begins with any of the given values:
$result = Str::startsWith('This is my name', ['This', 'That', 'There']);
// true
The Str::studly
method converts the given string to StudlyCase:
use Illuminate\Support\Str;
$converted = Str::studly('foo_bar');
// FooBar
The Str::substr
method returns the portion of string specified by the start and length parameters:
use Illuminate\Support\Str;
$converted = Str::substr('The Nexus -', 4, 7);
// Nexus
The Str::title
method converts the given string to Title Case:
use Illuminate\Support\Str;
$converted = Str::title('a nice title uses the correct case');
// A Nice Title Uses The Correct Case
The Str::ucfirst
method returns the given string with the first character capitalized:
use Illuminate\Support\Str;
$string = Str::ucfirst('foo bar');
// Foo bar
The Str::upper
method converts the given string to uppercase:
use Illuminate\Support\Str;
$string = Str::upper('Nexus');
// Nexus
The Str::uuid
method generates a UUID (version 4):
use Illuminate\Support\Str;
return (string) Str::uuid();
The Str::words
method limits the number of words in a string. An additional string may be passed to this method via its third argument to specify which string should be appended to the end of the truncated string:
use Illuminate\Support\Str;
return Str::words('Perfectly balanced, as all things should be.', 3, ' >>>');
// Perfectly balanced, as >>>