πŸ”€Strings

Laravel includes a variety of functions for manipulating string values. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.

__()

The __ function translates the given translation string or translation key using your language files:

If the specified translation string or key does not exist, the __ function will return the given value. So, using the example above, the __ function would return messages.welcome if that translation key does not exist.

class_basename()

The class_basename function returns the class name of the given class with the class's namespace removed:

e()

The e function runs PHP's htmlspecialchars function with the double_encode option set to true by default:

preg_replace_array()

The preg_replace_array function replaces a given pattern in the string sequentially using an array:

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:

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:

Str::apa()

The Str::apa method converts the given string to title case following the APA guidelines:

Str::ascii()

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

Str::before()

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

Str::beforeLast()

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

Str::between()

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

Str::betweenFirst()

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

Str::camel()

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

Str::charAt()

The Str::charAt method returns the character at the specified index. If the index is out of bounds, false is returned:

Str::chopStart()

The Str::chopStart method removes the first occurrence of the given value only if the value appears at the start of the string:

You may also pass an array as the second argument. If the string starts with any of the values in the array then that value will be removed from string:

Str::chopEnd()

The Str::chopEnd method removes the last occurrence of the given value only if the value appears at the end of the string:

You may also pass an array as the second argument. If the string ends with any of the values in the array then that value will be removed from string:

Str::contains()

The Str::contains method determines if the given string contains the given value. By default this method is case sensitive:

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

You may disable case sensitivity by setting the ignoreCase argument to true:

Str::containsAll()

The Str::containsAll method determines if the given string contains all of the values in a given array:

You may disable case sensitivity by setting the ignoreCase argument to true:

Str::doesntContain()

The Str::doesntContain method determines if the given string doesn't contain the given value. By default this method is case sensitive:

You may also pass an array of values to determine if the given string doesn't contain any of the values in the array:

You may disable case sensitivity by setting the ignoreCase argument to true:

Str::deduplicate()

The Str::deduplicate method replaces consecutive instances of a character with a single instance of that character in the given string. By default, the method deduplicates spaces:

You may specify a different character to deduplicate by passing it in as the second argument to the method:

Str::endsWith()

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

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

Str::excerpt()

The Str::excerpt method extracts an excerpt from a given string that matches the first instance of a phrase within that string:

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 define the string that will be prepended and appended to the truncated string:

Str::finish()

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

Str::headline()

The Str::headline method will convert strings delimited by casing, hyphens, or underscores into a space delimited string with each word's first letter capitalized:

Str::inlineMarkdown()

The Str::inlineMarkdown method converts GitHub flavored Markdown into inline HTML using CommonMark. However, unlike the markdown method, it does not wrap all generated HTML in a block-level element:

Markdown Security

By default, Markdown supports raw HTML, which will expose Cross-Site Scripting (XSS) vulnerabilities when used with raw user input. As per the CommonMark Security documentation, you may use the html_input option to either escape or strip raw HTML, and the allow_unsafe_links option to specify whether to allow unsafe links. If you need to allow some raw HTML, you should pass your compiled Markdown through an HTML Purifier:

Str::is()

The Str::is method determines if a given string matches a given pattern. Asterisks may be used as wildcard values:

You may disable case sensitivity by setting the ignoreCase argument to true:

Str::isAscii()

The Str::isAscii method determines if a given string is 7 bit ASCII:

Str::isJson()

The Str::isJson method determines if the given string is valid JSON:

Str::isUrl()

The Str::isUrl method determines if the given string is a valid URL:

The isUrl method considers a wide range of protocols as valid. However, you may specify the protocols that should be considered valid by providing them to the isUrl method:

Str::isUlid()

The Str::isUlid method determines if the given string is a valid ULID:

Str::isUuid()

The Str::isUuid method determines if the given string is a valid UUID:

Str::kebab()

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

Str::lcfirst()

The Str::lcfirst method returns the given string with the first character lowercased:

Str::length()

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

Str::limit()

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

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

If you would like to preserve complete words when truncating the string, you may utilize the preserveWords argument. When this argument is true, the string will be truncated to the nearest complete word boundary:

Str::lower()

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

Str::markdown()

The Str::markdown method converts GitHub flavored Markdown into HTML using CommonMark:

Markdown Security

By default, Markdown supports raw HTML, which will expose Cross-Site Scripting (XSS) vulnerabilities when used with raw user input. As per the CommonMark Security documentation, you may use the html_input option to either escape or strip raw HTML, and the allow_unsafe_links option to specify whether to allow unsafe links. If you need to allow some raw HTML, you should pass your compiled Markdown through an HTML Purifier:

Str::mask()

The Str::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:

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:

Str::orderedUuid()

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:

Str::padBoth()

The Str::padBoth method wraps PHP's str_pad function, padding both sides of a string with another string until the final string reaches a desired length:

Str::padLeft()

The Str::padLeft method wraps PHP's str_pad function, padding the left side of a string with another string until the final string reaches a desired length:

Str::padRight()

The Str::padRight method wraps PHP's str_pad function, padding the right side of a string with another string until the final string reaches a desired length:

Str::password()

The Str::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::plural()

The Str::plural method converts a singular word string to its plural form. This function supports any of the languages support by Laravel's pluralizer:

You may provide an integer as a second argument to the function to retrieve the singular or plural form of the string:

Str::pluralStudly()

The Str::pluralStudly method converts a singular word string formatted in studly caps case to its plural form. This function supports any of the languages support by Laravel's pluralizer:

You may provide an integer as a second argument to the function to retrieve the singular or plural form of the string:

Str::position()

The Str::position method returns the position of the first occurrence of a substring in a string. If the substring does not exist in the given string, false is returned:

Str::random()

The Str::random method generates a random string of the specified length. This function uses PHP's random_bytes function:

During testing, it may be useful to "fake" the value that is returned by the Str::random method. To accomplish this, you may use the createRandomStringsUsing method:

To instruct the random method to return to generating random strings normally, you may invoke the createRandomStringsNormally method:

Str::remove()

The Str::remove method removes the given value or array of values from the string:

You may also pass false as a third argument to the remove method to ignore case when removing strings.

Str::repeat()

The Str::repeat method repeats the given string:

Str::replace()

The Str::replace method replaces a given string within the string:

The replace method also accepts a caseSensitive argument. By default, the replace method is case sensitive:

Str::replaceArray()

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

Str::replaceFirst()

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

Str::replaceLast()

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

Str::replaceMatches()

The Str::replaceMatches method replaces all portions of a string matching a pattern with the given replacement string:

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:

Str::replaceStart()

The Str::replaceStart method replaces the first occurrence of the given value only if the value appears at the start of the string:

Str::replaceEnd()

The Str::replaceEnd method replaces the last occurrence of the given value only if the value appears at the end of the string:

Str::reverse()

The Str::reverse method reverses the given string:

Str::singular()

The Str::singular method converts a string to its singular form. This function supports any of the languages support by Laravel's pluralizer:

Str::slug()

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

Str::snake()

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

Str::squish()

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

Str::start()

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

Str::startsWith()

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

If an array of possible values is passed, the startsWith method will return true if the string begins with any of the given values:

Str::studly()

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

Str::substr()

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

Str::substrCount()

The Str::substrCount method returns the number of occurrences of a given value in the given string:

Str::substrReplace()

The Str::substrReplace method replaces text within a portion of a string, starting at the position specified by the third argument and replacing the number of characters specified by the fourth argument. Passing 0 to the method's fourth argument will insert the string at the specified position without replacing any of the existing characters in the string:

Str::swap()

The Str::swap method replaces multiple values in the given string using PHP's strtr function:

Str::take()

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

Str::title()

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

Str::toBase64()

The Str::toBase64 method converts the given string to Base64:

Str::transliterate()

The Str::transliterate method will attempt to convert a given string into its closest ASCII representation:

Str::trim()

The Str::trim method strips whitespace (or other characters) from the beginning and end of the given string. Unlike PHP's native trim function, the Str::trim method also removes unicode whitespace characters:

Str::ltrim()

The Str::ltrim method strips whitespace (or other characters) from the beginning of the given string. Unlike PHP's native ltrim function, the Str::ltrim method also removes unicode whitespace characters:

Str::rtrim()

The Str::rtrim method strips whitespace (or other characters) from the end of the given string. Unlike PHP's native rtrim function, the Str::rtrim method also removes unicode whitespace characters:

Str::ucfirst()

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

Str::ucsplit()

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

Str::upper()

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

Str::ulid()

The Str::ulid method generates a ULID, which is a compact, time-ordered unique identifier:

If you would like to retrieve a Illuminate\Support\Carbon date instance representing the date and time that a given ULID was created, you may use the createFromId method provided by Laravel's Carbon integration:

During testing, it may be useful to "fake" the value that is returned by the Str::ulid method. To accomplish this, you may use the createUlidsUsing method:

To instruct the ulid method to return to generating ULIDs normally, you may invoke the createUlidsNormally method:

Str::unwrap()

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

Str::uuid()

The Str::uuid method generates a UUID (version 4):

During testing, it may be useful to "fake" the value that is returned by the Str::uuid method. To accomplish this, you may use the createUuidsUsing method:

To instruct the uuid method to return to generating UUIDs normally, you may invoke the createUuidsNormally method:

Str::wordCount()

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

Str::wordWrap()

The Str::wordWrap method wraps a string to a given number of characters:

Str::words()

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:

Str::wrap()

The Str::wrap method wraps the given string with an additional string or pair of strings:

str()

The str function returns a new Illuminate\Support\Stringable instance of the given string. This function is equivalent to the Str::of method:

If no argument is provided to the str function, the function returns an instance of Illuminate\Support\Str:

trans()

The trans function translates the given translation key using your language files:

If the specified translation key does not exist, the trans function will return the given key. So, using the example above, the trans function would return messages.welcome if the translation key does not exist.

trans_choice()

The trans_choice function translates the given translation key with inflection:

If the specified translation key does not exist, the trans_choice function will return the given key. So, using the example above, the trans_choice function would return messages.notifications if the translation key does not exist.

Fluent strings provide a more fluent, object-oriented interface for working with string values, allowing you to chain multiple string operations together using a more readable syntax compared to traditional string operations.

after

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

afterLast

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:

apa

The apa method converts the given string to title case following the APA guidelines:

append

The append method appends the given values to the string:

ascii

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

basename

The basename method will return the trailing name component of the given string:

If needed, you may provide an "extension" that will be removed from the trailing component:

before

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

beforeLast

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

between

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

betweenFirst

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

camel

The camel method converts the given string to camelCase:

charAt

The charAt method returns the character at the specified index. If the index is out of bounds, false is returned:

classBasename

The classBasename method returns the class name of the given class with the class's namespace removed:

chopStart

The chopStart method removes the first occurrence of the given value only if the value appears at the start of the string:

You may also pass an array. If the string starts with any of the values in the array then that value will be removed from string:

chopEnd

The chopEnd method removes the last occurrence of the given value only if the value appears at the end of the string:

You may also pass an array. If the string ends with any of the values in the array then that value will be removed from string:

contains

The contains method determines if the given string contains the given value. By default this method is case sensitive:

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

You can disable case sensitivity by setting the ignoreCase argument to true:

containsAll

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

You can disable case sensitivity by setting the ignoreCase argument to true:

deduplicate

The deduplicate method replaces consecutive instances of a character with a single instance of that character in the given string. By default, the method deduplicates spaces:

You may specify a different character to deduplicate by passing it in as the second argument to the method:

dirname

The dirname method returns the parent directory portion of the given string:

If necessary, you may specify how many directory levels you wish to trim from the string:

endsWith

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

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

exactly

The exactly method determines if the given string is an exact match with another string:

excerpt

The excerpt method extracts an excerpt from the string that matches the first instance of a phrase within that string:

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:

explode

The explode method splits the string by the given delimiter and returns a collection containing each section of the split string:

finish

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

headline

The headline method will convert strings delimited by casing, hyphens, or underscores into a space delimited string with each word's first letter capitalized:

inlineMarkdown

The inlineMarkdown method converts GitHub flavored Markdown into inline HTML using CommonMark. However, unlike the markdown method, it does not wrap all generated HTML in a block-level element:

Markdown Security

By default, Markdown supports raw HTML, which will expose Cross-Site Scripting (XSS) vulnerabilities when used with raw user input. As per the CommonMark Security documentation, you may use the html_input option to either escape or strip raw HTML, and the allow_unsafe_links option to specify whether to allow unsafe links. If you need to allow some raw HTML, you should pass your compiled Markdown through an HTML Purifier:

is

The is method determines if a given string matches a given pattern. Asterisks may be used as wildcard values

isAscii

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

isEmpty

The isEmpty method determines if the given string is empty:

isNotEmpty

The isNotEmpty method determines if the given string is not empty:

isJson

The isJson method determines if a given string is valid JSON:

isUlid

The isUlid method determines if a given string is a ULID:

isUrl

The isUrl method determines if a given string is a URL:

The isUrl method considers a wide range of protocols as valid. However, you may specify the protocols that should be considered valid by providing them to the isUrl method:

isUuid

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

kebab

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

lcfirst

The lcfirst method returns the given string with the first character lowercased:

length

The length method returns the length of the given string:

limit

The limit method truncates the given string to the specified length:

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

If you would like to preserve complete words when truncating the string, you may utilize the preserveWords argument. When this argument is true, the string will be truncated to the nearest complete word boundary:

lower

The lower method converts the given string to lowercase:

markdown

The markdown method converts GitHub flavored Markdown into HTML:

Markdown Security

By default, Markdown supports raw HTML, which will expose Cross-Site Scripting (XSS) vulnerabilities when used with raw user input. As per the CommonMark Security documentation, you may use the html_input option to either escape or strip raw HTML, and the allow_unsafe_links option to specify whether to allow unsafe links. If you need to allow some raw HTML, you should pass your compiled Markdown through an HTML Purifier:

mask

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:

If needed, you may provide negative numbers as the third or fourth argument to the mask method, which will instruct the method to begin masking at the given distance from the end of the string:

match

The match method will return the portion of a string that matches a given regular expression pattern:

matchAll

The matchAll method will return a collection containing the portions of a string that match a given regular expression pattern:

If you specify a matching group within the expression, Laravel will return a collection of the first matching group's matches:

If no matches are found, an empty collection will be returned.

isMatch

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

newLine

The newLine method appends an "end of line" character to a string:

padBoth

The padBoth method wraps PHP's str_pad function, padding both sides of a string with another string until the final string reaches the desired length:

padLeft

The padLeft method wraps PHP's str_pad function, padding the left side of a string with another string until the final string reaches the desired length:

padRight

The padRight method wraps PHP's str_pad function, padding the right side of a string with another string until the final string reaches the desired length:

pipe

The pipe method allows you to transform the string by passing its current value to the given callable:

plural

The plural method converts a singular word string to its plural form. This function supports any of the languages support by Laravel's pluralizer:

You may provide an integer as a second argument to the function to retrieve the singular or plural form of the string:

position

The position method returns the position of the first occurrence of a substring in a string. If the substring does not exist within the string, false is returned:

prepend

The prepend method prepends the given values onto the string:

remove

The remove method removes the given value or array of values from the string:

You may also pass false as a second parameter to ignore case when removing strings.

repeat

The repeat method repeats the given string:

replace

The replace method replaces a given string within the string:

The replace method also accepts a caseSensitive argument. By default, the replace method is case sensitive:

replaceArray

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

replaceFirst

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

replaceLast

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

replaceMatches

The replaceMatches method replaces all portions of a string matching a pattern with the given replacement string:

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:

replaceStart

The replaceStart method replaces the first occurrence of the given value only if the value appears at the start of the string:

replaceEnd

The replaceEnd method replaces the last occurrence of the given value only if the value appears at the end of the string:

scan

The scan method parses input from a string into a collection according to a format supported by the sscanf PHP function:

singular

The singular method converts a string to its singular form. This function supports any of the languages support by Laravel's pluralizer:

slug

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

snake

The snake method converts the given string to snake_case:

split

The split method splits a string into a collection using a regular expression:

squish

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

start

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

startsWith

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

stripTags

The stripTags method removes all HTML and PHP tags from a string:

studly

The studly method converts the given string to StudlyCase:

substr

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

substrReplace

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

swap

The swap method replaces multiple values in the string using PHP's strtr function:

take

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

tap

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:

test

The test method determines if a string matches the given regular expression pattern:

title

The title method converts the given string to Title Case:

toBase64

The toBase64 method converts the given string to Base64:

toHtmlString

The toHtmlString method converts the given string to an instance of Illuminate\Support\HtmlString, which will not be escaped when rendered in Blade templates:

transliterate

The transliterate method will attempt to convert a given string into its closest ASCII representation:

trim

The trim method trims the given string. Unlike PHP's native trim function, Laravel's trim method also removes unicode whitespace characters:

ltrim

The ltrim method trims the left side of the string. Unlike PHP's native ltrim function, Laravel's ltrim method also removes unicode whitespace characters:

rtrim

The rtrim method trims the right side of the given string. Unlike PHP's native rtrim function, Laravel's rtrim method also removes unicode whitespace characters:

ucfirst

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

ucsplit

The ucsplit method splits the given string into a collection by uppercase characters:

unwrap

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

upper

The upper method converts the given string to uppercase:

when

The when method invokes the given closure if a given condition is true. The closure will receive the fluent string instance:

If necessary, you may pass another closure as the third parameter to the when method. This closure will execute if the condition parameter evaluates to false.

whenContains

The whenContains method invokes the given closure if the string contains the given value. The closure will receive the fluent string instance:

If necessary, you may pass another closure as the third parameter to the when method. This closure 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:

whenContainsAll

The whenContainsAll method invokes the given closure if the string contains all of the given sub-strings. The closure will receive the fluent string instance:

If necessary, you may pass another closure as the third parameter to the when method. This closure will execute if the condition parameter evaluates to false.

whenEmpty

The whenEmpty method invokes the given closure if the string is empty. If the closure returns a value, that value will also be returned by the whenEmpty method. If the closure does not return a value, the fluent string instance will be returned:

whenNotEmpty

The whenNotEmpty method invokes the given closure if the string is not empty. If the closure returns a value, that value will also be returned by the whenNotEmpty method. If the closure does not return a value, the fluent string instance will be returned:

whenStartsWith

The whenStartsWith method invokes the given closure if the string starts with the given sub-string. The closure will receive the fluent string instance:

whenEndsWith

The whenEndsWith method invokes the given closure if the string ends with the given sub-string. The closure will receive the fluent string instance:

whenExactly

The whenExactly method invokes the given closure if the string exactly matches the given string. The closure will receive the fluent string instance:

whenNotExactly

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:

whenIs

The whenIs method invokes the given closure if the string matches a given pattern. Asterisks may be used as wildcard values. The closure will receive the fluent string instance:

whenIsAscii

The whenIsAscii method invokes the given closure if the string is 7 bit ASCII. The closure will receive the fluent string instance:

whenIsUlid

The whenIsUlid method invokes the given closure if the string is a valid ULID. The closure will receive the fluent string instance:

whenIsUuid

The whenIsUuid method invokes the given closure if the string is a valid UUID. The closure will receive the fluent string instance:

whenTest

The whenTest method invokes the given closure if the string matches the given regular expression. The closure will receive the fluent string instance:

wordCount

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

words

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:

wrap

The wrap method wraps the given string with an additional string or pair of strings:

Last updated