The str function returns a new Stringable instance of the given string.
If no argument is given to the function, it returns an instance of Str.
str('Taylor');
// Stringable Object
str();
// Str ObjectThe of method returns a new Stringable instance of the given string.
Stringable.of('Taylor');
// Stringable ObjectThe 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:
Stringable.of('This is my name').after('This is');
// ' my name'The 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:
Stringable.of('App\\Http\\Controllers\\Controller').afterLast('\\');
// 'Controller'The append method appends the given values to the string:
Stringable.of('Taylor').append(' Otwell');
// 'Taylor Otwell'The ascii method will attempt to transliterate the string into an ASCII value:
Stringable.of('ü').ascii();
// 'u'The basename method will return the trailing name component of the given string:
Stringable.of('/foo/bar/baz').basename();
// 'baz'If needed, you may provide an "extension" that will be removed from the trailing component:
Stringable.of('/foo/bar/baz.jpg').basename('.jpg');
// 'baz'The before method returns everything before the given value in a string:
Stringable.of('This is my name').before('my name');
// 'This is 'The beforeLast method returns everything before the last occurrence of the given value in a string:
Stringable.of('This is my name').beforeLast('is');
// 'This 'The between method returns the portion of a string between two values:
Stringable.of('This is my name').between('This', 'name');
// ' is my 'The betweenFirst method returns the smallest possible portion of a string between two values:
Stringable.of('[a] bc [d]').betweenFirst('[', ']');
// 'a'The charAt method allows to get a character by index from a multibyte string:
Stringable.of('Hello, world!').charAt(1);
// 'e'The camel method converts the given string to camelCase:
Stringable.of('foo_bar').camel();
// 'fooBar'The contains method determines if the given string contains the given value. This method is case-sensitive:
Stringable.of('This is my name').contains('my');
// trueYou may also pass an array of values to determine if the given string contains any of the values in the array:
Stringable.of('This is my name').contains(['my', 'foo']);
// trueThe containsAll method determines if the given string contains all the values in the given array:
Stringable.of('This is my name').containsAll(['my', 'name']);
// trueThe convertCase method converts the given string to given mode:
Stringable.of('HeLLo WoRLD').convertCase(MB_CASE_LOWER);
// 'hello world'The dirname method returns the parent directory portion of the given string:
Stringable.of('/foo/bar/baz').dirname();
// '/foo/bar'If necessary, you may specify how many directory levels you wish to trim from the string:
Stringable.of('/foo/bar/baz').dirname(2);
// '/foo'The dd method dumps the string and ends execution of the script:
Stringable.of('This is my name').dd();
// Error: 'This is my name'If you do not want to halt the execution of your script, use the dump method instead.
The dump method dumps the string:
Stringable.of('This is my name').dump();
// 'This is my name'If you want to stop executing the script after dumping the variables, use the dd method instead.
The excerpt method extracts an excerpt from the string that matches the first instance of a phrase within that string:
Stringable.of('This is my name').excerpt('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:
Stringable.of('This is my name').excerpt('name', {
radius: 3,
omission: '(...) '
});
// '(...) my name'The endsWith method determines if the given string ends with the given value:
Stringable.of('This is my name').endsWith('name');
// trueYou may also pass an array of values to determine if the given string ends with any of the values in the array:
Stringable.of('This is my name').endsWith(['name', 'foo']);
// true
Stringable.of('This is my name').endsWith(['this', 'foo']);
// falseThe exactly method determines if the given string is an exact match with another string:
Stringable.of('Laravel').exactly('Laravel');
// trueThe explode method splits the string by the given delimiter and returns an array containing each section of the split string:
Stringable.of('foo bar baz').explode(' ');
// ['foo', 'bar', 'baz']The finish method adds a single instance of the given value to a string if it does not already end with that value:
Stringable.of('this/string').finish('/');
// 'this/string/'
Stringable.of('this/string/').finish('/');
// 'this/string/'The flushCache method removes all strings from the casing caches.
Stringable.flushCache();The headline method will convert strings delimited by casing, hyphens, or underscores into a space delimited string with each word's first letter capitalized:
Stringable.of('steve_jobs').headline();
// 'Steve Jobs'
Stringable.of('EmailNotificationSent').headline();
// 'Email Notification Sent'The is method determines if a given string matches a given pattern. Asterisks may be used as wildcard values
Stringable.of('foobar').is('foo*');
// true
Stringable.of('foobar').is(/baz*/);
// falseThe isAscii method determines if a given string is an ASCII string:
Stringable.of('Taylor').isAscii();
// true
Stringable.of('ü').isAscii();
// falseThe isEmpty method determines if the given string is empty:
Stringable.of(' ').trim().isEmpty();
// true
Stringable.of('Laravel').trim().isEmpty();
// falseThe isNotEmpty method determines if the given string is not empty:
Stringable.of(' ').trim().isNotEmpty();
// false
Stringable.of('Laravel').trim().isNotEmpty();
// trueThe isJson method determines if a given string is valid JSON:
Stringable.of('[1,2,3]').isJson();
// true
Stringable.of('{"first": "John", "last": "Doe"}').isJson();
// true
Stringable.of('{first: "John", last: "Doe"}').isJson();
// falseThe isUrl method determines if a given string is a valid URL:
Stringable.of('https://example.com').isUrl();
// true
Stringable.of('example').isUrl();
// falseThe isUlid method determines if a given string is a valid ULID:
Stringable.of('01ARZ3NDEKTSV4RRFFQ69G5FAV').isUlid();
// true
Stringable.of('Taylor').isUlid();
// falseThe isUuid method determines if a given string is a UUID:
Stringable.of('5ace9ab9-e9cf-4ec6-a19d-5881212a452c').isUuid();
// true
Stringable.of('Taylor').isUuid();
// falseThe isMatch method will return true if the string matches a given regular expression:
Stringable.of('foo bar').isMatch(/foo (.*)/);
// true
Stringable.of('laravel').isMatch(/foo (.*)/);
// falseThe kebab method converts the given string to kebab-case:
Stringable.of('fooBar').kebab();
// 'foo-bar'The lcfirst method returns the given string with the first character lowercase:
Stringable.of('Foo Bar').lcfirst();
// 'foo Bar'The length method returns the length of the given string:
Stringable.of('Laravel').length();
// 7The limit method truncates the given string to the specified length:
Stringable.of('The quick brown fox jumps over the lazy dog').limit(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:
Stringable.of('The quick brown fox jumps over the lazy dog').limit(20, ' (...)');
// 'The quick brown fox (...)'If you want to keep whole words when truncating a string, you can use the third argument. When this argument is true, the string will be truncated to the nearest full word boundary:
Stringable.of('The quick brown fox jumps over the lazy dog').limit(12, '...', true);
// 'The quick...'The lower method converts the given string to lowercase:
Stringable.of('LARAVEL').lower();
// 'laravel'The ltrim method trims the left side of the string:
Stringable.of(' Laravel ').ltrim();
// 'Laravel '
Stringable.of('/Laravel/').ltrim('/');
// 'Laravel/'The markdown method converts GitHub flavored Markdown into HTML:
Stringable.of('# Laravel').markdown();
// <h1>Laravel</h1>
Stringable.of('# Taylor <b>Otwell</b>').markdown({'html_input': 'strip'});
// <h1>Taylor Otwell</h1>The inlineMarkdown method converts GitHub flavored Markdown into inline HTML.
However, unlike the markdown method, it does not wrap all generated HTML in a block-level element:
Stringable.of('**Laravel**').inlineMarkdown();
// <strong>Laravel</strong>The mask method 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:
Stringable.of('taylor@example.com').mask('*', 3);
// 'tay***************'If needed, you provide a negative number as the third argument to the mask method, which will instruct the method
to begin masking at the given distance from the end of the string:
Stringable.of('taylor@example.com').mask('*', -15, 3);
// 'tay***@example.com'The match method will return the portion of a string that matches a given regular expression pattern:
Stringable.of('foo bar').match('bar');
// 'bar'
Stringable.of('foo bar').match(/foo (.*)/);
// 'bar'The matchAll method will return an array containing the portions of a string that match a given regular expression pattern:
Stringable.of('bar foo bar').matchAll('bar');
// ['bar', 'bar']If you specify a matching group within the expression, package will return an array of that group's matches:
Stringable.of('bar fun bar fly').matchAll(/f(\w*)/);
// ['un', 'ly']If no matches are found, an empty array will be returned.
The newLine method appends an "end of line" character to a string:
Stringable.of('Laravel').newLine().append('Framework');
// 'Laravel
// Framework'The padBoth method wraps both sides of a string with another string until the final string reaches the desired length:
Stringable.of('James').padBoth(10, '_');
// '__James___'
Stringable.of('James').padBoth(10);
// ' James 'The padLeft method wraps the left side of a string with another string until the final string reaches the desired length:
Stringable.of('James').padLeft(10, '-=');
// '-=-=-James'
Stringable.of('James').padLeft(10);
// ' James'The padRight method wraps the right side of a string with another string until the final string reaches the desired length:
Stringable.of('James').padRight(10, '-');
// 'James-----'
Stringable.of('James').padRight(10);
// 'James 'The parseCallback method parse to an array a Class@method style callback into class and method:
Stringable.of('Class@method').parseCallback();
// ['Class', 'method']The pipe method allows you to transform the string by passing its current value to the given callable:
Stringable.of('Laravel').pipe('md5').prepend('Checksum: ');
// 'Checksum: a5c95b86291ea299fcbe64458ed12702'
Stringable.of('foo').pipe(str => 'bar');
// 'bar'The prepend method prepends the given values onto the string:
Stringable.of('Framework').prepend('Laravel ');
// 'Laravel Framework'The remove method removes the given value or array of values from the string:
Stringable.of('Arkansas is quite beautiful!').remove('quite');
// 'Arkansas is beautiful!'You may also pass false as a second parameter to ignore case when removing strings.
The repeat method repeats the given value N times:
Stringable.of('a').repeat('5');
// 'aaaaa'The replace method replaces a given string within the string:
Stringable.of('Laravel 6.x').replace('6.x', '7.x');
// 'Laravel 7.x'The replace method also accepts a caseSensitive argument. By default, the replace method is case-sensitive:
Stringable.of('Laravel 10.x').replace('10.X', '11.x', false);
// 'Laravel 11.x'The replaceArray method replaces a given value in the string sequentially using an array:
Stringable.of('The event will take place between ? and ?').replaceArray('?', ['8:30', '9:00']);
// 'The event will take place between 8:30 and 9:00'The replaceEnd method replaces the last occurrence of the given value only if the value appears at the start of the string:
Stringable.of('Hello World').replaceEnd('World', 'Laravel');
// Hello LaravelThe replaceFirst method replaces the first occurrence of a given value in a string:
Stringable.of('the quick brown fox jumps over the lazy dog').replaceFirst('the', 'a');
// 'a quick brown fox jumps over the lazy dog'The replaceLast method replaces the last occurrence of a given value in a string:
Stringable.of('the quick brown fox jumps over the lazy dog').replaceLast('the', 'a');
// 'the quick brown fox jumps over a lazy dog'The replaceStart method replaces the first occurrence of the given value only if the value appears at the start of the string:
Stringable.of('Hello World').replaceStart('Hello', 'Laravel');
// Laravel WorldThe replaceMatches method replaces all portions of a string matching a pattern with the given replacement string:
Stringable.of('(+1) 501-555-1000').replaceMatches(/[^A-Za-z0-9]++/, '')
// '15015551000'The replaceMatches method also accepts a closure that will be invoked with each portion of the string matching the given pattern,
allowing you to perform the replacement logic within the closure and return the replaced value:
Stringable.of('123').replaceMatches(/\d/, match => '['+match[0]+']');
// '[1][2][3]'The reverse method reverses the given string:
Stringable.of('Hello World').reverse();
// 'dlroW olleH'The rtrim method trims the right side of the given string:
Stringable.of(' Laravel ').rtrim();
// ' Laravel'
Stringable.of('/Laravel/').rtrim('/');
// '/Laravel'The scan method parses input from a string into an array similar to scan PHP function:
Stringable.of('filename.jpg').scan('%[^.].%s');
// ['filename', 'jpg']The slug method generates a URL friendly "slug" from the given string:
Stringable.of('Laravel Framework').slug('-');
// 'laravel-framework'The snake method converts the given string to snake_case:
Stringable.of('fooBar').snake();
// 'foo_bar'The split method splits a string into an array using a regular expression:
Stringable.of('one, two, three').split(/[\s,]+/);
// ["one", "two", "three"]The squish method removes all extraneous white space from a string, including extraneous white space between words:
Stringable.of(' laravel framework ').squish();
// 'laravel framework'The start method adds a single instance of the given value to a string if it does not already start with that value:
Stringable.of('this/string').start('/');
// '/this/string'
Stringable.of('/this/string').start('/');
// '/this/string'The startsWith method determines if the given string begins with the given value:
Stringable.of('This is my name').startsWith('This');
// trueThe stripTags method strips HTML and PHP tags from the given string:
Stringable.of('before<br>after').stripTags();
// 'beforeafter'The studly method converts the given string to StudlyCase:
Stringable.of('foo_bar').studly();
// 'FooBar'The substr method returns the portion of the string specified by the given start and length parameters:
Stringable.of('Laravel Framework').substr(8);
// 'Framework'
Stringable.of('Laravel Framework').substr(8, 5);
// 'Frame'The substrCount method returns the number of occurrences of a given value in the given string:
Stringable.of('If you like ice cream, you will like snow cones.').substrCount('like');
// 2The substrReplace method 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 third argument. Passing 0 to the method's third argument
will insert the string at the specified position without replacing any of the existing characters in the string:
Stringable.of('1300').substrReplace(':', 2);
// '13':
Stringable.of('The Framework').substrReplace(' Laravel', 3, 0);
// 'The Laravel Framework'The swap method replaces multiple values in the string similar to PHP strtr function:
Stringable.of('Tacos are great!').swap({
'Tacos': 'Burritos',
'great': 'fantastic',
});
// 'Burritos are fantastic!'The take method returns a specified number of characters from the beginning of a string:
Stringable.of('Build something amazing!').take(5);
// 'Build'The tap method passes the string to the given closure, allowing you to examine and interact with the string while
not affecting the string itself. The original string is returned by the tap method regardless of what is returned by the closure:
Stringable.of('Laravel')
.append(' Framework')
.tap((str) => {
console.log('String after append: ' + str);
})
.upper();
// 'LARAVEL FRAMEWORK'The test method determines if a string matches the given regular expression pattern:
Stringable.of('Laravel Framework').test(/Laravel/);
// trueThe title method converts the given string to Title Case:
Stringable.of('a nice title uses the correct case').title();
// 'A Nice Title Uses The Correct Case'The toHtmlString method converts the string instance to an instance of Element, which may be displayed in HTML:
Stringable.of('Nuno Maduro').toHtmlString();The toString method returns the underlying string value.
Stringable.of('foo').toString();
// 'foo'The toInteger method returns string value as an integer.
Stringable.of('123').toInteger();
// 123The toFloat method returns string value as a float.
Stringable.of('1').toFloat();
// 1.0'The toBoolean method returns string value as a boolean.
Stringable.of('yes').toBoolean();
// trueThe toDate method returns string value as a Date.
Stringable.of('2020-01-01 16:30:25').toDate();
// Date(2020-01-01 16:30:25)The trim method trims the given string:
Stringable.of(' Laravel ').trim();
// 'Laravel'
Stringable.of('/Laravel/').trim('/');
// 'Laravel'The ucfirst method returns the given string with the first character capitalized:
Stringable.of('foo bar').ucfirst();
// 'Foo bar'The ucsplit method splits the given string into an array by uppercase characters:
Stringable.of('Foo Bar').ucsplit();
// ['Foo', 'Bar']The unless method invokes the given function if a given condition is false.
The function will receive the fluent string instance:
Stringable.of('Taylor').unless(false, (str) => str.append(' false'));
// 'unless false'The upper method converts the given string to uppercase:
Stringable.of('laravel').upper();
// 'LARAVEL'The when method invokes the given function if a given condition is true. The function will receive the fluent string instance:
Stringable.of('Taylor').when(true, (str) => str.append(' Otwell'));
// 'Taylor Otwell'If necessary, you may pass another function as the third parameter to when method. This function will execute if the condition parameter evaluates to false.
The whenContains method invokes the given function if the string contains the given value. The function will receive the fluent string instance:
Stringable.of('tony stark').whenContains('tony', (str) => str.title());
// 'Tony Stark'If necessary, you may pass another function as the third parameter to when method. This function will execute if the string does not contain the given value.
You may also pass an array of values to determine if the given string contains any of the values in the array:
Stringable.of('tony stark').whenContains(['tony', 'hulk'], (str) => str.title());
// 'Tony Stark'The whenContainsAll method invokes the given function if the string contains all the given sub-strings.
The function will receive the fluent string instance:
Stringable.of('tony stark').whenContainsAll(['tony', 'stark'], (str) => str.title());
// 'Tony Stark'If necessary, you may pass another closure as the third parameter to when method.
This function will execute if the condition parameter evaluates to false.
The whenEmpty method invokes the given function if the string is empty. If the function returns a value,
that value will also be returned by the whenEmpty method. If the function does not return a value, the fluent string instance will be returned:
Stringable.of('').whenEmpty((str) => str.trim().prepend('Laravel'));
// 'Laravel'The whenNotEmpty method invokes the given function if the string is not empty. If the function returns a value,
that value will also be returned by the whenNotEmpty method. If the function does not return a value,
the fluent string instance will be returned:
Stringable.of('Framework').whenNotEmpty(str => str.prepend('Laravel '));
// 'Laravel Framework'The whenStartsWith method invokes the given function if the string starts with the given sub-string.
The function will receive the fluent string instance:
Stringable.of('disney world').whenStartsWith('disney', (str) => str.title());
// 'Disney World'The whenEndsWith method invokes the given function if the string ends with the given sub-string.
The function will receive the fluent string instance:
Stringable.of('disney world').whenEndsWith('world', (str) => str.title());
// 'Disney World'The whenExactly method invokes the given function if the string exactly matches the given string.
The function will receive the fluent string instance:
Stringable.of('laravel').whenExactly('laravel', (str) => str.title());
// 'Laravel'The whenNotExactly method invokes the given closure if the string does not exactly match the given string.
The closure will receive the fluent string instance:
Stringable.of('framework').whenNotExactly('laravel', (str) => str.title());
// 'Framework'The whenIs method invokes the given function if the string matches a given pattern. Asterisks may be used as wildcard values.
The function will receive the fluent string instance:
Stringable.of('foo/bar').whenIs('foo/*', (str) => str.append('/baz'));
// 'foo/bar/baz'The whenIsAscii method invokes the given function if the string is 7-bit ASCII.
The function will receive the fluent string instance:
Stringable.of('A').whenIsAscii((str) => str.prepend('Ascii:'));
// 'Ascii: A'The whenIsUlid method invokes the given function if the string is a valid ULID.
The function will receive the fluent string instance:
Stringable.of('01gd6r360bp37zj17nxb55yv40').whenIsUlid((str) => str.substr(0, 8));
// '01gd6r36'The whenIsUuid method invokes the given function if the string is a valid UUID.
The function will receive the fluent string instance:
Stringable.of('2cdc7039-65a6-4ac7-8e5d-d554a98e7b15').whenIsUuid((str) => str.prepend('Uuid: '));
// 'Uuid: 2cdc7039-65a6-4ac7-8e5d-d554a98e7b15'The whenTest method invokes the given function if the string matches the given regular expression.
The function will receive the fluent string instance:
Stringable.of('laravel framework').whenTest(/laravel/, (str) => str.title());
// 'Laravel Framework'The wordCount method returns the number of words that a string contains:
Stringable.of('Hello, world!').wordCount();
// 2The wordWrap method wraps a string to a given number of characters:
Stringable.of('The quick brown fox jumped over the lazy dog').wordWrap(20, "<br />\n");
/*
The quick brown fox<br />
jumped over the lazy<br />
dog.
*/The words method limits the number of words in a string. If necessary, you may specify an additional string that will be appended to the truncated string:
Stringable.of('Perfectly balanced, as all things should be.').words(3, ' >>>');
// 'Perfectly balanced, as >>>'The wrap method wraps the string with the given strings:
Stringable.of('is').wrap('This ', ' me!');
// 'This is me!'The unwrap method removes the specified strings from the beginning and end of a given string:
Stringable.of('-Laravel-').unwrap('- ');
// 'Laravel'The value method returns the underlying string value.
Stringable.of('foo').value();
// 'foo'