The of function creates new instance of Stringable class:
Str.of('foo bar');
// Stringable ObjectThe after function returns everything after the given value in a string.
The entire string will be returned if the value does not exist within the string:
Str.after('This is my name', 'This is');
// ' my name'The afterLast function 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:
Str.afterLast('App\\Http\\Controllers\\Controller', '\\');
// 'Controller'The ascii function will attempt to transliterate the string into an ASCII value:
Str.ascii('ü');
// 'u'The before function returns everything before the given value in a string:
Str.before('This is my name', 'my name');
// 'This is 'The beforeLast function returns everything before the last occurrence of the given value in a string:
Str.beforeLast('This is my name', 'is');
// 'This 'The between function returns the portion of a string between two values:
Str.between('This is my name', 'This', 'name');
// ' is my 'The betweenFirst function returns the smallest possible portion of a string between two values:
Str.betweenFirst('[a] bc [d]', '[', ']');
// 'a'The camel function converts the given string to camelCase:
Str.camel('foo_bar');
// 'fooBar'The charAt function allows to get a character by index from a multibyte string:
Str.charAt('Hello, world!', 1);
// 'e'The contains function determines if the given string contains the given value. This function is case-sensitive:
Str.contains('This is my name', 'my');
// trueYou may also pass an array of values to determine if the given string contains any of the values in the array:
Str.contains('This is my name', ['my', 'foo']);
// trueThe containsAll function determines if the given string contains all the values in the given array:
Str.containsAll('This is my name', ['my', 'name']);
// trueThe convertCase function converts the given string to given mode:
Str.convertCase('HeLLo WoRLD', MB_CASE_LOWER);
// 'hello world'The endsWith function determines if the given string ends with the given value:
Str.endsWith('This is my name', 'name');
// trueYou may also pass an array of values to determine if the given string ends with any of the values in the array:
Str.endsWith('This is my name', ['name', 'foo']);
// true
Str.endsWith('This is my name', ['this', 'foo']);
// falseThe excerpt function extracts an excerpt from the string that matches the first instance of a phrase within that string:
Str.excerpt('This is my name', 'my', {
radius: 3
});
// '...is my na...'The radius option, which defaults to 100, allows you to define the number of characters that should appear on each side of the truncated string.
In addition, you may use the omission option to change the string that will be prepended and appended to the truncated string:
Str.excerpt('This is my name', 'name', {
radius: 3,
omission: '(...) '
});
// '(...) my name'The explode function splits the string by the given delimiter and returns an array containing each section of the split string:
Str.explode('foo bar baz', ' ');
// ['foo', 'bar', 'baz']The finish function adds a single instance of the given value to a string if it does not already end with that value:
Str.finish('this/string', '/');
// 'this/string/'
Str.finish('this/string/', '/');
// 'this/string/'The wrap function wraps the string with the given strings:
Str.wrap('is', 'This ', ' me!');
// 'This is me!'The is function determines if a given string matches a given pattern. Asterisks may be used as wildcard values
Str.is('foo*', 'foobar');
// true
Str.is(/baz*/, 'foobar');
// falseThe isAscii function determines if a given string is an ASCII string:
Str.isAscii('Taylor');
// true
Str.isAscii('ü');
// falseThe isJson function determines if a given string is valid JSON:
Str.isJson('[1,2,3]');
// true
Str.isJson('{"first": "John", "last": "Doe"}');
// true
Str.isJson('{first: "John", last: "Doe"}');
// falseThe isUrl function determines if a given string is a valid URL:
Str.isUrl('https://example.com');
// true
Str.isUrl('example');
// falseThe isUlid function determines if a given string is a valid ULID:
Str.isUlid('01ARZ3NDEKTSV4RRFFQ69G5FAV');
// true
Str.isUlid('Taylor');
// falseThe isUuid function determines if a given string is a UUID:
Str.isUuid('5ace9ab9-e9cf-4ec6-a19d-5881212a452c');
// true
Str.isUuid('Taylor');
// falseThe isMatch function will return true if the string matches a given regular expression:
Str.isMatch(/foo (.*)/, 'foo bar');
// true
Str.isMatch(/foo (.*)/, 'laravel');
// falseThe kebab function converts the given string to kebab-case:
Str.kebab('fooBar');
// 'foo-bar'The length function returns the length of the given string:
Str.length('Laravel');
// 7The limit function truncates the given string to the specified length:
Str.limit('The quick brown fox jumps over the lazy dog', 20);
// 'The quick brown fox...'You may also pass a second argument to change the string that will be appended to the end of the truncated string:
Str.limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');
// 'The quick brown fox (...)'The lower function converts the given string to lowercase:
Str.lower('LARAVEL');
// 'laravel'The words function limits the number of words in a string. If necessary, you may specify an additional string that will be appended to the truncated string:
Str.words('Perfectly balanced, as all things should be.', 3, ' >>>');
// 'Perfectly balanced, as >>>'The markdown function converts GitHub flavored Markdown into HTML:
Str.markdown('# Laravel');
// <h1>Laravel</h1>
Str.markdown('# Taylor <b>Otwell</b>', {'html_input': 'strip'});
// <h1>Taylor Otwell</h1>The inlineMarkdown function converts GitHub flavored Markdown into inline HTML.
However, unlike the markdown function, it does not wrap all generated HTML in a block-level element:
Str.inlineMarkdown('**Laravel**');
// <strong>Laravel</strong>The mask function masks a portion of a string with a repeated character, and may be used to obfuscate
segments of strings such as email addresses and phone numbers:
Str.mask('taylor@example.com', '*', 3);
// 'tay***************'If needed, you provide a negative number as the third argument to the mask function, which will instruct the function
to begin masking at the given distance from the end of the string:
Str.mask('taylor@example.com', '*', -15, 3);
// 'tay***@example.com'The match function will return the portion of a string that matches a given regular expression pattern:
Str.match('bar', 'foo bar');
// 'bar'
Str.match(/foo (.*)/, 'foo bar');
// 'bar'The matchAll function will return an array containing the portions of a string that match a given regular expression pattern:
Str.matchAll('bar', 'bar foo bar');
// ['bar', 'bar']If you specify a matching group within the expression, package will return an array of that group's matches:
Str.matchAll(/f(\w*)/, 'bar fun bar fly');
// ['un', 'ly']The padBoth function wraps both sides of a string with another string until the final string reaches the desired length:
Str.padBoth('James', 10, '_');
// '__James___'
Str.padBoth('James', 10);
// ' James 'The padLeft function wraps the left side of a string with another string until the final string reaches the desired length:
Str.padLeft('James', 10, '-=');
// '-=-=-James'
Str.padLeft('James', 10);
// ' James'The padRight function wraps the right side of a string with another string until the final string reaches the desired length:
Str.padRight('James', 10, '-');
// 'James-----'
Str.padRight('James', 10);
// 'James 'The parseCallback function parse to an array a Class@method style callback into class and method:
Str.parseCallback('Class@method');
// ['Class', 'method']The password method may be used to generate a secure, random password of a given length. The password will consist
of a combination of letters, numbers, symbols, and spaces. By default, passwords are 32 characters long.
Str.password();
// 'EbJo2vE-AS:U,$%_gkrV4n,q~1xy/-_4'
Str.password(12);
// 'qwuar>#V|i]N'The random function generates a random string of the specified length:
Str.random(40);The createRandomStringsUsing function allows to intercept and control the random string generation.
Str.createRandomStringsUsing((length) => `xyz|${length}`);
Str.random(7);
// 'xyz:7'The createRandomStringsUsingSequence function allows to set a sequence that will be used to generate random strings.
Str.createRandomStringsUsingSequence({0: 'x', 2: 'y', 3: 'z'});
Str.random();
// 'x'
Str.random();
// random String
Str.random();
// 'y'The createRandomStringsNormally function resets to default random string generator.
Str.createRandomStringsUsing((length) => `xyz|${length}`);
Str.createRandomStringsNormally();
Str.random(7);
// random 7 charactersThe repeat function repeats the given value N times:
Str.repeat('a', '5');
// 'aaaaa'The replaceArray function replaces a given value in the string sequentially using an array:
Str.replaceArray('?', ['8:30', '9:00'], 'The event will take place between ? and ?');
// 'The event will take place between 8:30 and 9:00'The replace function replaces a given string within the string:
Str.replace('6.x', '7.x', 'Laravel 6.x');
// 'Laravel 7.x'The replaceEnd function replaces the last occurrence of the given value only if the value appears at the start of the string:
Str.replaceEnd('World', 'Laravel', 'Hello World');
// Hello LaravelThe replaceFirst function replaces the first occurrence of a given value in a string:
Str.replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');
// 'a quick brown fox jumps over the lazy dog'The replaceLast function replaces the last occurrence of a given value in a string:
Str.replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');
// 'the quick brown fox jumps over a lazy dog'The replaceStart function replaces the first occurrence of the given value only if the value appears at the start of the string:
Str.replaceStart('Hello', 'Laravel', 'Hello World');
// Laravel WorldThe remove function removes the given value or array of values from the string:
Str.remove('quite', 'Arkansas is quite beautiful!');
// 'Arkansas is beautiful!'You may also pass false as a third parameter to ignore case when removing strings.
The reverse function reverses the given string:
Str.reverse('Hello World');
// 'dlroW olleH'The start function adds a single instance of the given value to a string if it does not already start with that value:
Str.start('this/string', '/');
// '/this/string'
Str.start('/this/string', '/');
// '/this/string'The upper function converts the given string to uppercase:
Str.upper('laravel');
// 'LARAVEL'The take function returns a specified number of characters from the beginning of a string:
Str.take('Build something amazing!', 5);
// 'Build'The title function converts the given string to Title Case:
Str.title('a nice title uses the correct case');
// 'A Nice Title Uses The Correct Case'The headline function will convert strings delimited by casing, hyphens, or underscores into a space delimited string with each word's first letter capitalized:
Str.headline('steve_jobs');
// 'Steve Jobs'
Str.headline('EmailNotificationSent');
// 'Email Notification Sent'The slug function generates a URL friendly "slug" from the given string:
Str.slug('Laravel Framework', '-');
// 'laravel-framework'The snake function converts the given string to snake_case:
Str.snake('fooBar');
// 'foo_bar'The squish function removes all extraneous white space from a string, including extraneous white space between words:
Str.squish(' laravel framework ');
// 'laravel framework'The startsWith function determines if the given string begins with the given value:
Str.startsWith('This is my name', 'This');
// trueThe stripTags function strips HTML and PHP tags from the given string:
Str.stripTags('before<br>after');
// 'beforeafter'The studly function converts the given string to StudlyCase:
Str.studly('foo_bar');
// 'FooBar'The substr function returns the portion of string specified by the start and length parameters:
Str.substr('The Laravel Framework', 4, 7);
// 'Laravel'The substrCount function returns the number of occurrences of a given value in the given string:
Str.substrCount('If you like ice cream, you will like snow cones.', 'like');
// 2The substrReplace function replaces text within a portion of a string, starting at the position specified by the second argument
and replacing the number of characters specified by the fourth argument. Passing 0 to the function's fourth argument
will insert the string at the specified position without replacing any of the existing characters in the string:
Str.substrReplace('1300', ':', 2);
// '13':
Str.substrReplace('The Framework', ' Laravel', 3, 0);
// 'The Laravel Framework'The swap function replaces multiple values in the string similar to PHP strtr function:
Str.swap({
'Tacos': 'Burritos',
'great': 'fantastic',
}, 'Tacos are great!');
// 'Burritos are fantastic!'The trim function trims the given string:
Str.trim(' Laravel ');
// 'Laravel'
Str.trim('/Laravel/', '/');
// 'Laravel'The ltrim function trims the left side of the string:
Str.ltrim(' Laravel ');
// 'Laravel '
Str.ltrim('/Laravel/', '/');
// 'Laravel/'The rtrim function trims the right side of the given string:
Str.trim(' Laravel ');
// ' Laravel'
Str.rtrim('/Laravel/', '/');
// '/Laravel'The lcfirst function returns the given string with the first character lowercase:
Str.lcfirst('Foo Bar');
// 'foo Bar'The ucfirst function returns the given string with the first character capitalized:
Str.ucfirst('foo bar');
// 'Foo bar'The ucsplit function splits the given string into an array by uppercase characters:
Str.ucsplit('Foo Bar');
// ['Foo', 'Bar']The wordCount function returns the number of words that a string contains:
Str.wordCount('Hello, world!');
// 2The wordWrap function wraps a string to a given number of characters:
Str.wordWrap('The quick brown fox jumped over the lazy dog', 20, "<br />\n");
/*
The quick brown fox<br />
jumped over the lazy<br />
dog.
*/The uuid function generates a UUID (version 4):
Str.uuid();
// Stringable objectThe ulid function generates a ULID:
Str.ulid();
// Stringable objectThe preg_quote function quote regular expression characters:
Str.preg_quote('*RRRING* Hello?');
// '\*RRRING\* Hello\?'The unwrap function removes the specified strings from the beginning and end of a given string:
Str.unwrap('-Laravel-', '- ');
// 'Laravel'The flushCache function removes all strings from the casing caches.
Str.flushCache();