Skip to content

Latest commit

 

History

History
732 lines (589 loc) · 16.6 KB

statics.md

File metadata and controls

732 lines (589 loc) · 16.6 KB

Static Methods

of

The of function creates new instance of Stringable class:

Str.of('foo bar');

// Stringable Object

after

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

afterLast

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'

ascii

The ascii function will attempt to transliterate the string into an ASCII value:

Str.ascii('ü');

// 'u'

before

The before function returns everything before the given value in a string:

Str.before('This is my name', 'my name');

// 'This is '

beforeLast

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

Str.beforeLast('This is my name', 'is');

// 'This '

between

The between function returns the portion of a string between two values:

Str.between('This is my name', 'This', 'name');

// ' is my '

betweenFirst

The betweenFirst function returns the smallest possible portion of a string between two values:

Str.betweenFirst('[a] bc [d]', '[', ']');

// 'a'

camel

The camel function converts the given string to camelCase:

Str.camel('foo_bar');

// 'fooBar'

charAt

The charAt function allows to get a character by index from a multibyte string:

Str.charAt('Hello, world!', 1);

// 'e'

contains

The contains function determines if the given string contains the given value. This function is case-sensitive:

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:

Str.contains('This is my name', ['my', 'foo']);

// true

containsAll

The containsAll function determines if the given string contains all the values in the given array:

Str.containsAll('This is my name', ['my', 'name']);

// true

convertCase

The convertCase function converts the given string to given mode:

Str.convertCase('HeLLo WoRLD', MB_CASE_LOWER);

// 'hello world'

endsWith

The endsWith function determines if the given string ends with the given value:

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:

Str.endsWith('This is my name', ['name', 'foo']);

// true

Str.endsWith('This is my name', ['this', 'foo']);

// false

excerpt

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

explode

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

finish

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

wrap

The wrap function wraps the string with the given strings:

Str.wrap('is', 'This ', ' me!');

// 'This is me!'

is

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');

// false

isAscii

The isAscii function determines if a given string is an ASCII string:

Str.isAscii('Taylor');

// true

Str.isAscii('ü');

// false

isJson

The 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"}');

// false

isUrl

The isUrl function determines if a given string is a valid URL:

Str.isUrl('https://example.com');

// true

Str.isUrl('example');

// false

isUlid

The isUlid function determines if a given string is a valid ULID:

Str.isUlid('01ARZ3NDEKTSV4RRFFQ69G5FAV');

// true

Str.isUlid('Taylor');

// false

isUuid

The isUuid function determines if a given string is a UUID:

Str.isUuid('5ace9ab9-e9cf-4ec6-a19d-5881212a452c');

// true

Str.isUuid('Taylor');

// false

isMatch

The isMatch function will return true if the string matches a given regular expression:

Str.isMatch(/foo (.*)/, 'foo bar');

// true

Str.isMatch(/foo (.*)/, 'laravel');

// false

kebab

The kebab function converts the given string to kebab-case:

Str.kebab('fooBar');

// 'foo-bar'

length

The length function returns the length of the given string:

Str.length('Laravel');

// 7

limit

The 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 (...)'

lower

The lower function converts the given string to lowercase:

Str.lower('LARAVEL');

// 'laravel'

words

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

markdown

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>

inlineMarkdown

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>

mask

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('[email protected]', '*', 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('[email protected]', '*', -15, 3);

// 'tay***@example.com'

match

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'

matchAll

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

padBoth

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   '

padLeft

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'

padRight

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     '

parseCallback

The parseCallback function parse to an array a Class@method style callback into class and method:

Str.parseCallback('Class@method');

// ['Class', 'method']

password

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'

random

The random function generates a random string of the specified length:

Str.random(40);

createRandomStringsUsing

The createRandomStringsUsing function allows to intercept and control the random string generation.

Str.createRandomStringsUsing((length) => `xyz|${length}`);
Str.random(7);

// 'xyz:7'

createRandomStringsUsingSequence

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'

createRandomStringsNormally

The createRandomStringsNormally function resets to default random string generator.

Str.createRandomStringsUsing((length) => `xyz|${length}`);
Str.createRandomStringsNormally();
Str.random(7);

// random 7 characters

repeat

The repeat function repeats the given value N times:

Str.repeat('a', '5');

// 'aaaaa'

replaceArray

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'

replace

The replace function replaces a given string within the string:

Str.replace('6.x', '7.x', 'Laravel 6.x');

// 'Laravel 7.x'

replaceEnd

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 Laravel

replaceFirst

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

replaceLast

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'

replaceStart

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 World

remove

The 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.

reverse

The reverse function reverses the given string:

Str.reverse('Hello World');

// 'dlroW olleH'

start

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'

upper

The upper function converts the given string to uppercase:

Str.upper('laravel');

// 'LARAVEL'

take

The take function returns a specified number of characters from the beginning of a string:

Str.take('Build something amazing!', 5);

// 'Build'

title

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'

headline

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'

slug

The slug function generates a URL friendly "slug" from the given string:

Str.slug('Laravel Framework', '-');

// 'laravel-framework'

snake

The snake function converts the given string to snake_case:

Str.snake('fooBar');

// 'foo_bar'

squish

The squish function removes all extraneous white space from a string, including extraneous white space between words:

Str.squish('    laravel    framework    ');

// 'laravel framework'

startsWith

The startsWith function determines if the given string begins with the given value:

Str.startsWith('This is my name', 'This');

// true

stripTags

The stripTags function strips HTML and PHP tags from the given string:

Str.stripTags('before<br>after');

// 'beforeafter'

studly

The studly function converts the given string to StudlyCase:

Str.studly('foo_bar');

// 'FooBar'

substr

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

Str.substr('The Laravel Framework', 4, 7);

// 'Laravel'

substrCount

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');

// 2

substrReplace

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

swap

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

trim

The trim function trims the given string:

Str.trim('  Laravel  ');

// 'Laravel'

Str.trim('/Laravel/', '/');

// 'Laravel'

ltrim

The ltrim function trims the left side of the string:

Str.ltrim('  Laravel  ');

// 'Laravel  '

Str.ltrim('/Laravel/', '/');

// 'Laravel/'

rtrim

The rtrim function trims the right side of the given string:

Str.trim('  Laravel  ');

// '  Laravel'

Str.rtrim('/Laravel/', '/');

// '/Laravel'

lcfirst

The lcfirst function returns the given string with the first character lowercase:

Str.lcfirst('Foo Bar');

// 'foo Bar'

ucfirst

The ucfirst function returns the given string with the first character capitalized:

Str.ucfirst('foo bar');

// 'Foo bar'

ucsplit

The ucsplit function splits the given string into an array by uppercase characters:

Str.ucsplit('Foo Bar');

// ['Foo', 'Bar']

wordCount

The wordCount function returns the number of words that a string contains:

Str.wordCount('Hello, world!'); 

// 2

wordWrap

The 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.
*/

uuid

The uuid function generates a UUID (version 4):

Str.uuid();

// Stringable object

ulid

The ulid function generates a ULID:

Str.ulid();

// Stringable object

preg_quote

The preg_quote function quote regular expression characters:

Str.preg_quote('*RRRING* Hello?');

// '\*RRRING\* Hello\?'

unwrap

The unwrap function removes the specified strings from the beginning and end of a given string:

Str.unwrap('-Laravel-', '- ');

// 'Laravel'

flushCache

The flushCache function removes all strings from the casing caches.

Str.flushCache();