π€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.
The class_basename function returns the class name of the given class with the class's namespace removed:
The e function runs PHP's htmlspecialchars function with the double_encode option set to true by default:
The preg_replace_array function replaces a given pattern in the string sequentially using an array:
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:
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:
The Str::apa method converts the given string to title case following the APA guidelines:
The Str::ascii method will attempt to transliterate the string into an ASCII value:
The Str::before method returns everything before the given value in a string:
The Str::beforeLast method returns everything before the last occurrence of the given value in a string:
The Str::between method returns the portion of a string between two values:
The Str::betweenFirst method returns the smallest possible portion of a string between two values:
The Str::camel method converts the given string to camelCase:
The Str::charAt method returns the character at the specified index. If the index is out of bounds, false is returned:
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:
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:
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:
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:
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:
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:
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:
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:
The Str::finish method adds a single instance of the given value to a string if it does not already end with that value:
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:
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:
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:
The Str::isAscii method determines if a given string is 7 bit ASCII:
The Str::isJson method determines if the given string is valid JSON:
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:
The Str::isUlid method determines if the given string is a valid ULID:
The Str::isUuid method determines if the given string is a valid UUID:
The Str::kebab method converts the given string to kebab-case:
The Str::lcfirst method returns the given string with the first character lowercased:
The Str::length method returns the length of the given string:
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:
The Str::lower method converts the given string to lowercase:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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.
The Str::repeat method repeats the given string:
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:
The Str::replaceArray method replaces a given value in the string sequentially using an array:
The Str::replaceFirst method replaces the first occurrence of a given value in a string:
The Str::replaceLast method replaces the last occurrence of a given value in a string:
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:
The Str::replaceStart method replaces the first occurrence of the given value only if the value appears at the start of the string:
The Str::replaceEnd method replaces the last occurrence of the given value only if the value appears at the end of the string:
The Str::reverse method reverses the given string:
The Str::singular method converts a string to its singular form. This function supports any of the languages support by Laravel's pluralizer:
The Str::slug method generates a URL friendly "slug" from the given string:
The Str::snake method converts the given string to snake_case:
The Str::squish method removes all extraneous white space from a string, including extraneous white space between words:
The Str::start method adds a single instance of the given value to a string if it does not already start with that value:
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:
The Str::studly method converts the given string to StudlyCase:
The Str::substr method returns the portion of string specified by the start and length parameters:
The Str::substrCount method returns the number of occurrences of a given value in the given string:
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:
The Str::swap method replaces multiple values in the given string using PHP's strtr function:
The Str::take method returns a specified number of characters from the beginning of a string:
The Str::title method converts the given string to Title Case:
The Str::toBase64 method converts the given string to Base64:
The Str::transliterate method will attempt to convert a given string into its closest ASCII representation:
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:
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:
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:
The Str::ucfirst method returns the given string with the first character capitalized:
The Str::ucsplit method splits the given string into an array by uppercase characters:
The Str::upper method converts the given string to uppercase:
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:
The Str::unwrap method removes the specified strings from the beginning and end of a given string:
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:
The Str::wordCount method returns the number of words that a string contains:
The Str::wordWrap method wraps a string to a given number of characters:
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:
The Str::wrap method wraps the given string with an additional string or pair of strings:
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:
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.
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.
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:
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:
The apa method converts the given string to title case following the APA guidelines:
The append method appends the given values to the string:
The ascii method will attempt to transliterate the string into an ASCII value:
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:
The before method returns everything before the given value in a string:
The beforeLast method returns everything before the last occurrence of the given value in a string:
The between method returns the portion of a string between two values:
The betweenFirst method returns the smallest possible portion of a string between two values:
The camel method converts the given string to camelCase:
The charAt method returns the character at the specified index. If the index is out of bounds, false is returned:
The classBasename method returns the class name of the given class with the class's namespace removed:
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:
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:
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:
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:
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:
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:
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:
The exactly method determines if the given string is an exact match with another string:
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:
The explode method splits the string by the given delimiter and returns a collection containing each section of the split string:
The finish method adds a single instance of the given value to a string if it does not already end with that value:
The headline method will convert strings delimited by casing, hyphens, or underscores into a space delimited string with each word's first letter capitalized:
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:
The is method determines if a given string matches a given pattern. Asterisks may be used as wildcard values
The isAscii method determines if a given string is an ASCII string:
The isEmpty method determines if the given string is empty:
The isNotEmpty method determines if the given string is not empty:
The isJson method determines if a given string is valid JSON:
The isUlid method determines if a given string is a ULID:
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:
The isUuid method determines if a given string is a UUID:
The kebab method converts the given string to kebab-case:
The lcfirst method returns the given string with the first character lowercased:
The length method returns the length of the given string:
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:
The lower method converts the given string to lowercase:
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:
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:
The match method will return the portion of a string that matches a given regular expression pattern:
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.
The isMatch method will return true if the string matches a given regular expression:
The newLine method appends an "end of line" character to a string:
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:
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:
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:
The pipe method allows you to transform the string by passing its current value to the given callable:
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:
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:
The prepend method prepends the given values onto the string:
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.
The repeat method repeats the given string:
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:
The replaceArray method replaces a given value in the string sequentially using an array:
The replaceFirst method replaces the first occurrence of a given value in a string:
The replaceLast method replaces the last occurrence of a given value in a string:
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:
The replaceStart method replaces the first occurrence of the given value only if the value appears at the start of the string:
The replaceEnd method replaces the last occurrence of the given value only if the value appears at the end of the string:
The scan method parses input from a string into a collection according to a format supported by the sscanf PHP function:
The singular method converts a string to its singular form. This function supports any of the languages support by Laravel's pluralizer:
The slug method generates a URL friendly "slug" from the given string:
The snake method converts the given string to snake_case:
The split method splits a string into a collection using a regular expression:
The squish method removes all extraneous white space from a string, including extraneous white space between words:
The start method adds a single instance of the given value to a string if it does not already start with that value:
The startsWith method determines if the given string begins with the given value:
The stripTags method removes all HTML and PHP tags from a string:
The studly method converts the given string to StudlyCase:
The substr method returns the portion of the string specified by the given start and length parameters:
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:
The swap method replaces multiple values in the string using PHP's strtr function:
The take method returns a specified number of characters from the beginning of the string:
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:
The test method determines if a string matches the given regular expression pattern:
The title method converts the given string to Title Case:
The toBase64 method converts the given string to Base64:
The toHtmlString method converts the given string to an instance of Illuminate\Support\HtmlString, which will not be escaped when rendered in Blade templates:
The transliterate method will attempt to convert a given string into its closest ASCII representation:
The trim method trims the given string. Unlike PHP's native trim function, Laravel's trim method also removes unicode whitespace characters:
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:
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:
The ucfirst method returns the given string with the first character capitalized:
The ucsplit method splits the given string into a collection by uppercase characters:
The unwrap method removes the specified strings from the beginning and end of a given string:
The upper method converts the given string to uppercase:
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.
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:
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.
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:
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:
The whenStartsWith method invokes the given closure if the string starts with the given sub-string. The closure will receive the fluent string instance:
The whenEndsWith method invokes the given closure if the string ends with the given sub-string. The closure will receive the fluent string instance:
The whenExactly method invokes the given closure if the string exactly matches the given string. The closure will receive the fluent string instance:
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:
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:
The whenIsAscii method invokes the given closure if the string is 7 bit ASCII. The closure will receive the fluent string instance:
The whenIsUlid method invokes the given closure if the string is a valid ULID. The closure will receive the fluent string instance:
The whenIsUuid method invokes the given closure if the string is a valid UUID. The closure will receive the fluent string instance:
The whenTest method invokes the given closure if the string matches the given regular expression. The closure will receive the fluent string instance:
The wordCount method returns the number of words that a string contains:
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:
The wrap method wraps the given string with an additional string or pair of strings:
Last updated