Str
class Str (View source)
Traits
| Macroable |
Properties
| static protected array | $macros | The registered string macros. | from Macroable |
| static protected array | $snakeCache | The cache of snake-cased words. | |
| static protected array | $camelCache | The cache of camel-cased words. | |
| static protected array | $studlyCache | The cache of studly-cased words. | |
| static protected callable|null | $uuidFactory | The callback that should be used to generate UUIDs. | |
| static protected callable|null | $ulidFactory | The callback that should be used to generate ULIDs. | |
| static protected callable|null | $randomStringFactory | The callback that should be used to generate random strings. |
Methods
| static void | macro(string $name, object|callable $macro)
Register a custom macro. | from Macroable |
| static void | mixin(object $mixin, bool $replace = true)
Mix another object into the class. | from Macroable |
| static bool | hasMacro(string $name)
Checks if macro is registered. | from Macroable |
| static void | flushMacros()
Flush the existing macros. | from Macroable |
| static mixed | __callStatic(string $method, array $parameters)
Dynamically handle calls to the class. | from Macroable |
| mixed | __call(string $method, array $parameters)
Dynamically handle calls to the class. | from Macroable |
| static Stringable | of(string $string)
Get a new stringable object from the given string. | |
| static string | after(string $subject, string $search)
Return the remainder of a string after the first occurrence of a given value. | |
| static string | afterLast(string $subject, string $search)
Return the remainder of a string after the last occurrence of a given value. | |
| static string | ascii(string $value, string $language = 'en')
Transliterate a UTF-8 value to ASCII. | |
| static string | transliterate(string $string, string|null $unknown = '?', bool|null $strict = false)
Transliterate a string to its closest ASCII representation. | |
| static string | before(string $subject, string $search)
Get the portion of a string before the first occurrence of a given value. | |
| static string | beforeLast(string $subject, string $search)
Get the portion of a string before the last occurrence of a given value. | |
| static string | between(string $subject, string $from, string $to)
Get the portion of a string between two given values. | |
| static string | betweenFirst(string $subject, string $from, string $to)
Get the smallest possible portion of a string between two given values. | |
| static string | camel(string $value)
Convert a value to camel case. | |
| static string|false | charAt(string $subject, int $index)
Get the character at the specified index. | |
| static string | chopStart(string $subject, string|array $needle)
Remove the given string(s) if it exists at the start of the haystack. | |
| static string | chopEnd(string $subject, string|array $needle)
Remove the given string(s) if it exists at the end of the haystack. | |
| static bool | contains(string $haystack, string|iterable<string> $needles, bool $ignoreCase = false)
Determine if a given string contains a given substring. | |
| static bool | containsAll(string $haystack, iterable<string> $needles, bool $ignoreCase = false)
Determine if a given string contains all array values. | |
| static string | convertCase(string $string, int $mode = MB_CASE_FOLD, string|null $encoding = 'UTF-8')
Convert the case of a string. | |
| static bool | endsWith(string $haystack, string|iterable<string> $needles)
Determine if a given string ends with a given substring. | |
| static string|null | excerpt(string $text, string $phrase = '', array $options = [])
Extracts an excerpt from text that matches the first instance of a phrase. | |
| static string | finish(string $value, string $cap)
Cap a string with a single instance of a given value. | |
| static string | wrap(string $value, string $before, string|null $after = null)
Wrap the string with the given strings. | |
| static string | unwrap(string $value, string $before, string|null $after = null)
Unwrap the string with the given strings. | |
| static bool | is(string|iterable<string> $pattern, string $value)
Determine if a given string matches a given pattern. | |
| static bool | isAscii(string $value)
Determine if a given string is 7 bit ASCII. | |
| static bool | isJson(mixed $value)
Determine if a given value is valid JSON. | |
| static bool | isUrl(mixed $value, array $protocols = [])
Determine if a given value is a valid URL. | |
| static bool | isUuid(mixed $value)
Determine if a given value is a valid UUID. | |
| static bool | isUlid(mixed $value)
Determine if a given value is a valid ULID. | |
| static string | kebab(string $value)
Convert a string to kebab case. | |
| static int | length(string $value, string|null $encoding = null)
Return the length of the given string. | |
| static string | limit(string $value, int $limit = 100, string $end = '...')
Limit the number of characters in a string. | |
| static string | lower(string $value)
Convert the given string to lower-case. | |
| static string | words(string $value, int $words = 100, string $end = '...')
Limit the number of words in a string. | |
| static string | markdown(string $string, array $options = [], array $extensions = [])
Converts GitHub flavored Markdown into HTML. | |
| static string | inlineMarkdown(string $string, array $options = [])
Converts inline Markdown into HTML. | |
| static string | mask(string $string, string $character, int $index, int|null $length = null, string $encoding = 'UTF-8')
Masks a portion of a string with a repeated character. | |
| static string | match(string $pattern, string $subject)
Get the string matching the given pattern. | |
| static bool | isMatch(string|iterable<string> $pattern, string $value)
Determine if a given string matches a given pattern. | |
| static Collection | matchAll(string $pattern, string $subject)
Get the string matching the given pattern. | |
| static string | numbers(string $value)
Remove all non-numeric characters from a string. | |
| static string | padBoth(string $value, int $length, string $pad = ' ')
Pad both sides of a string with another. | |
| static string | padLeft(string $value, int $length, string $pad = ' ')
Pad the left side of a string with another. | |
| static string | padRight(string $value, int $length, string $pad = ' ')
Pad the right side of a string with another. | |
| static array<int,string|null> | parseCallback(string $callback, string|null $default = null)
Parse a Class[@]method style callback into class and method. | |
| static string | plural(string $value, int|array|Countable $count = 2)
Get the plural form of an English word. | |
| static string | pluralStudly(string $value, int|array|Countable $count = 2)
Pluralize the last word of an English, studly caps case string. | |
| static string | password(int $length = 32, bool $letters = true, bool $numbers = true, bool $symbols = true, bool $spaces = false)
Generate a random, secure password. | |
| static int|false | position(string $haystack, string $needle, int $offset = 0, string|null $encoding = null)
Find the multi-byte safe position of the first occurrence of a given substring in a string. | |
| static string | random(int $length = 16)
Generate a more truly "random" alpha-numeric string. | |
| static void | createRandomStringsUsing(callable|null $factory = null)
Set the callable that will be used to generate random strings. | |
| static void | createRandomStringsUsingSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to generate random strings. | |
| static void | createRandomStringsNormally()
Indicate that random strings should be created normally and not using a custom factory. | |
| static string | repeat(string $string, int $times)
Repeat the given string. | |
| static string | replaceArray(string $search, iterable<string> $replace, string $subject)
Replace a given value in the string sequentially with an array. | |
| static string|string[] | replace(string|iterable<string> $search, string|iterable<string> $replace, string|iterable<string> $subject, bool $caseSensitive = true)
Replace the given value in the given string. | |
| static string | replaceFirst(string $search, string $replace, string $subject)
Replace the first occurrence of a given value in the string. | |
| static string | replaceStart(string $search, string $replace, string $subject)
Replace the first occurrence of the given value if it appears at the start of the string. | |
| static string | replaceLast(string $search, string $replace, string $subject)
Replace the last occurrence of a given value in the string. | |
| static string | replaceEnd(string $search, string $replace, string $subject)
Replace the last occurrence of a given value if it appears at the end of the string. | |
| static string|string[]|null | replaceMatches(array|string $pattern, Closure|string[]|string $replace, array|string $subject, int $limit = -1)
Replace the patterns matching the given regular expression. | |
| static string | remove(string|iterable<string> $search, string|iterable<string> $subject, bool $caseSensitive = true)
Remove any occurrence of the given string in the subject. | |
| static string | reverse(string $value)
Reverse the given string. | |
| static string | start(string $value, string $prefix)
Begin a string with a single instance of a given value. | |
| static string | upper(string $value)
Convert the given string to upper-case. | |
| static string | title(string $value)
Convert the given string to proper case. | |
| static string | headline(string $value)
Convert the given string to proper case for each word. | |
| static string | apa(string $value)
Convert the given string to APA-style title case. | |
| static string | singular(string $value)
Get the singular form of an English word. | |
| static string | slug(string $title, string $separator = '-', string|null $language = 'en', array<string,string> $dictionary = ['@' => 'at'])
Generate a URL friendly "slug" from a given string. | |
| static string | snake(string $value, string $delimiter = '_')
Convert a string to snake case. | |
| static string | trim(string $value, string|null $charlist = null)
Remove all whitespace from both ends of a string. | |
| static string | ltrim(string $value, string|null $charlist = null)
Remove all whitespace from the beginning of a string. | |
| static string | rtrim(string $value, string|null $charlist = null)
Remove all whitespace from the end of a string. | |
| static string | squish(string $value)
Remove all "extra" blank space from the given string. | |
| static bool | startsWith(string $haystack, string|iterable<string> $needles)
Determine if a given string starts with a given substring. | |
| static string | studly(string $value)
Convert a value to studly caps case. | |
| static string | substr(string $string, int $start, int|null $length = null, string $encoding = 'UTF-8')
Returns the portion of the string specified by the start and length parameters. | |
| static int | substrCount(string $haystack, string $needle, int $offset = 0, int|null $length = null)
Returns the number of substring occurrences. | |
| static string|string[] | substrReplace(string|string[] $string, string|string[] $replace, int|int[] $offset = 0, int|int[]|null $length = null)
Replace text within a portion of a string. | |
| static string | swap(array $map, string $subject)
Swap multiple keywords in a string with other keywords. | |
| static string | take(string $string, int $limit)
Take the first or last {$limit} characters of a string. | |
| static string | toBase64(string $string)
Convert the given string to Base64 encoding. | |
| static string|false | fromBase64(string $string, bool $strict = false)
Decode the given Base64 encoded string. | |
| static string | lcfirst(string $string)
Make a string's first character lowercase. | |
| static string | ucfirst(string $string)
Make a string's first character uppercase. | |
| static string[] | ucsplit(string $string)
Split a string into pieces by uppercase characters. | |
| static int | wordCount(string $string, string|null $characters = null)
Get the number of words a string contains. | |
| static string | wordWrap(string $string, int $characters = 75, string $break = "\n", bool $cutLongWords = false)
Wrap a string to a given number of characters. | |
| static UuidInterface | uuid()
Generate a UUID (version 4). | |
| static UuidInterface | orderedUuid()
Generate a time-ordered UUID. | |
| static void | createUuidsUsing(callable|null $factory = null)
Set the callable that will be used to generate UUIDs. | |
| static void | createUuidsUsingSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to generate UUIDs. | |
| static UuidInterface | freezeUuids(Closure|null $callback = null)
Always return the same UUID when generating new UUIDs. | |
| static void | createUuidsNormally()
Indicate that UUIDs should be created normally and not using a custom factory. | |
| static Ulid | ulid(DateTimeInterface|null $time = null)
Generate a ULID. | |
| static void | createUlidsNormally()
Indicate that ULIDs should be created normally and not using a custom factory. | |
| static void | createUlidsUsing(callable|null $factory = null)
Set the callable that will be used to generate ULIDs. | |
| static void | createUlidsUsingSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to generate ULIDs. | |
| static Ulid | freezeUlids(Closure|null $callback = null)
Always return the same ULID when generating new ULIDs. | |
| static void | flushCache()
Remove all strings from the casing caches. |
Details
static void macro(string $name, object|callable $macro)
Register a custom macro.
static void mixin(object $mixin, bool $replace = true)
Mix another object into the class.
static bool hasMacro(string $name)
Checks if macro is registered.
static void flushMacros()
Flush the existing macros.
static mixed __callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
mixed __call(string $method, array $parameters)
Dynamically handle calls to the class.
static Stringable of(string $string)
Get a new stringable object from the given string.
static string after(string $subject, string $search)
Return the remainder of a string after the first occurrence of a given value.
static string afterLast(string $subject, string $search)
Return the remainder of a string after the last occurrence of a given value.
static string ascii(string $value, string $language = 'en')
Transliterate a UTF-8 value to ASCII.
static string transliterate(string $string, string|null $unknown = '?', bool|null $strict = false)
Transliterate a string to its closest ASCII representation.
static string before(string $subject, string $search)
Get the portion of a string before the first occurrence of a given value.
static string beforeLast(string $subject, string $search)
Get the portion of a string before the last occurrence of a given value.
static string between(string $subject, string $from, string $to)
Get the portion of a string between two given values.
static string betweenFirst(string $subject, string $from, string $to)
Get the smallest possible portion of a string between two given values.
static string camel(string $value)
Convert a value to camel case.
static string|false charAt(string $subject, int $index)
Get the character at the specified index.
static string chopStart(string $subject, string|array $needle)
Remove the given string(s) if it exists at the start of the haystack.
static string chopEnd(string $subject, string|array $needle)
Remove the given string(s) if it exists at the end of the haystack.
static bool contains(string $haystack, string|iterable<string> $needles, bool $ignoreCase = false)
Determine if a given string contains a given substring.
static bool containsAll(string $haystack, iterable<string> $needles, bool $ignoreCase = false)
Determine if a given string contains all array values.
static string convertCase(string $string, int $mode = MB_CASE_FOLD, string|null $encoding = 'UTF-8')
Convert the case of a string.
static bool endsWith(string $haystack, string|iterable<string> $needles)
Determine if a given string ends with a given substring.
static string|null excerpt(string $text, string $phrase = '', array $options = [])
Extracts an excerpt from text that matches the first instance of a phrase.
static string finish(string $value, string $cap)
Cap a string with a single instance of a given value.
static string wrap(string $value, string $before, string|null $after = null)
Wrap the string with the given strings.
static string unwrap(string $value, string $before, string|null $after = null)
Unwrap the string with the given strings.
static bool is(string|iterable<string> $pattern, string $value)
Determine if a given string matches a given pattern.
static bool isAscii(string $value)
Determine if a given string is 7 bit ASCII.
static bool isJson(mixed $value)
Determine if a given value is valid JSON.
static bool isUrl(mixed $value, array $protocols = [])
Determine if a given value is a valid URL.
static bool isUuid(mixed $value)
Determine if a given value is a valid UUID.
static bool isUlid(mixed $value)
Determine if a given value is a valid ULID.
static string kebab(string $value)
Convert a string to kebab case.
static int length(string $value, string|null $encoding = null)
Return the length of the given string.
static string limit(string $value, int $limit = 100, string $end = '...')
Limit the number of characters in a string.
static string lower(string $value)
Convert the given string to lower-case.
static string words(string $value, int $words = 100, string $end = '...')
Limit the number of words in a string.
static string markdown(string $string, array $options = [], array $extensions = [])
Converts GitHub flavored Markdown into HTML.
static string inlineMarkdown(string $string, array $options = [])
Converts inline Markdown into HTML.
static string mask(string $string, string $character, int $index, int|null $length = null, string $encoding = 'UTF-8')
Masks a portion of a string with a repeated character.
static string match(string $pattern, string $subject)
Get the string matching the given pattern.
static bool isMatch(string|iterable<string> $pattern, string $value)
Determine if a given string matches a given pattern.
static Collection matchAll(string $pattern, string $subject)
Get the string matching the given pattern.
static string numbers(string $value)
Remove all non-numeric characters from a string.
static string padBoth(string $value, int $length, string $pad = ' ')
Pad both sides of a string with another.
static string padLeft(string $value, int $length, string $pad = ' ')
Pad the left side of a string with another.
static string padRight(string $value, int $length, string $pad = ' ')
Pad the right side of a string with another.
static array<int,string|null> parseCallback(string $callback, string|null $default = null)
Parse a Class[@]method style callback into class and method.
static string plural(string $value, int|array|Countable $count = 2)
Get the plural form of an English word.
static string pluralStudly(string $value, int|array|Countable $count = 2)
Pluralize the last word of an English, studly caps case string.
static string password(int $length = 32, bool $letters = true, bool $numbers = true, bool $symbols = true, bool $spaces = false)
Generate a random, secure password.
static int|false position(string $haystack, string $needle, int $offset = 0, string|null $encoding = null)
Find the multi-byte safe position of the first occurrence of a given substring in a string.
static string random(int $length = 16)
Generate a more truly "random" alpha-numeric string.
static void createRandomStringsUsing(callable|null $factory = null)
Set the callable that will be used to generate random strings.
static void createRandomStringsUsingSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to generate random strings.
static void createRandomStringsNormally()
Indicate that random strings should be created normally and not using a custom factory.
static string repeat(string $string, int $times)
Repeat the given string.
static string replaceArray(string $search, iterable<string> $replace, string $subject)
Replace a given value in the string sequentially with an array.
static string|string[] replace(string|iterable<string> $search, string|iterable<string> $replace, string|iterable<string> $subject, bool $caseSensitive = true)
Replace the given value in the given string.
static string replaceFirst(string $search, string $replace, string $subject)
Replace the first occurrence of a given value in the string.
static string replaceStart(string $search, string $replace, string $subject)
Replace the first occurrence of the given value if it appears at the start of the string.
static string replaceLast(string $search, string $replace, string $subject)
Replace the last occurrence of a given value in the string.
static string replaceEnd(string $search, string $replace, string $subject)
Replace the last occurrence of a given value if it appears at the end of the string.
static string|string[]|null replaceMatches(array|string $pattern, Closure|string[]|string $replace, array|string $subject, int $limit = -1)
Replace the patterns matching the given regular expression.
static string remove(string|iterable<string> $search, string|iterable<string> $subject, bool $caseSensitive = true)
Remove any occurrence of the given string in the subject.
static string reverse(string $value)
Reverse the given string.
static string start(string $value, string $prefix)
Begin a string with a single instance of a given value.
static string upper(string $value)
Convert the given string to upper-case.
static string title(string $value)
Convert the given string to proper case.
static string headline(string $value)
Convert the given string to proper case for each word.
static string apa(string $value)
Convert the given string to APA-style title case.
See: https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case
static string singular(string $value)
Get the singular form of an English word.
static string slug(string $title, string $separator = '-', string|null $language = 'en', array<string,string> $dictionary = ['@' => 'at'])
Generate a URL friendly "slug" from a given string.
static string snake(string $value, string $delimiter = '_')
Convert a string to snake case.
static string trim(string $value, string|null $charlist = null)
Remove all whitespace from both ends of a string.
static string ltrim(string $value, string|null $charlist = null)
Remove all whitespace from the beginning of a string.
static string rtrim(string $value, string|null $charlist = null)
Remove all whitespace from the end of a string.
static string squish(string $value)
Remove all "extra" blank space from the given string.
static bool startsWith(string $haystack, string|iterable<string> $needles)
Determine if a given string starts with a given substring.
static string studly(string $value)
Convert a value to studly caps case.
static string substr(string $string, int $start, int|null $length = null, string $encoding = 'UTF-8')
Returns the portion of the string specified by the start and length parameters.
static int substrCount(string $haystack, string $needle, int $offset = 0, int|null $length = null)
Returns the number of substring occurrences.
static string|string[] substrReplace(string|string[] $string, string|string[] $replace, int|int[] $offset = 0, int|int[]|null $length = null)
Replace text within a portion of a string.
static string swap(array $map, string $subject)
Swap multiple keywords in a string with other keywords.
static string take(string $string, int $limit)
Take the first or last {$limit} characters of a string.
static string toBase64(string $string)
Convert the given string to Base64 encoding.
static string|false fromBase64(string $string, bool $strict = false)
Decode the given Base64 encoded string.
static string lcfirst(string $string)
Make a string's first character lowercase.
static string ucfirst(string $string)
Make a string's first character uppercase.
static string[] ucsplit(string $string)
Split a string into pieces by uppercase characters.
static int wordCount(string $string, string|null $characters = null)
Get the number of words a string contains.
static string wordWrap(string $string, int $characters = 75, string $break = "\n", bool $cutLongWords = false)
Wrap a string to a given number of characters.
static UuidInterface uuid()
Generate a UUID (version 4).
static UuidInterface orderedUuid()
Generate a time-ordered UUID.
static void createUuidsUsing(callable|null $factory = null)
Set the callable that will be used to generate UUIDs.
static void createUuidsUsingSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to generate UUIDs.
static UuidInterface freezeUuids(Closure|null $callback = null)
Always return the same UUID when generating new UUIDs.
static void createUuidsNormally()
Indicate that UUIDs should be created normally and not using a custom factory.
static Ulid ulid(DateTimeInterface|null $time = null)
Generate a ULID.
static void createUlidsNormally()
Indicate that ULIDs should be created normally and not using a custom factory.
static void createUlidsUsing(callable|null $factory = null)
Set the callable that will be used to generate ULIDs.
static void createUlidsUsingSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to generate ULIDs.
static Ulid freezeUlids(Closure|null $callback = null)
Always return the same ULID when generating new ULIDs.
static void flushCache()
Remove all strings from the casing caches.
© Taylor Otwell
Licensed under the MIT License.
Laravel is a trademark of Taylor Otwell.
https://laravel.com/api/11.x/Illuminate/Support/Str.html