Str
class Str
Traits
Constants
| INVISIBLE_CHARACTERS |
The list of characters that are considered "invisible" in strings. |
Properties
| static protected array | $macros | The registered string macros. |
from Macroable |
| static protected null|Closure(): string|Uuid | $uuidFactory | The callback that should be used to generate UUIDs. |
|
| static protected null|Ulid | $ulidFactory | The callback that should be used to generate ULIDs. |
|
| static protected null|Closure(int): string | $randomStringFactory | The callback that should be used to generate random strings. |
Methods
Mix another object into the class.
Dynamically handle calls to the class.
Dynamically handle calls to the class.
Get a new stringable object from the given string.
Translate the given message and get a new stringable object.
Return the remainder of a string after the first occurrence of a given value.
Return the remainder of a string after the last occurrence of a given value.
Transliterate a UTF-8 value to ASCII.
Transliterate a string to its closest ASCII representation.
Get the portion of a string before the first occurrence of a given value.
Get the portion of a string before the last occurrence of a given value.
Get the portion of a string between two given values.
Get the smallest possible portion of a string between two given values.
Convert a value to camel case.
Get the character at the specified index.
Remove the given string(s) if it exists at the start of the haystack.
Remove the given string(s) if it exists at the end of the haystack.
Determine if a given string contains a given substring.
Determine if a given string contains all array values.
Determine if a given string doesn't contain a given substring.
Convert the case of a string.
Replace consecutive instances of a given character with a single character in the given string.
Determine if a given string ends with a given substring.
Determine if a given string doesn't end with a given substring.
Extract an excerpt from text that matches the first instance of a phrase.
Cap a string with a single instance of a given value.
Wrap the string with the given strings.
Unwrap the string with the given strings.
Determine if a given string matches a given pattern.
Determine if a given value is valid JSON.
Determine if a given value is a valid URL.
Determine if a given value is a valid UUID.
Determine if a given value is a valid ULID.
Convert a string to kebab case.
Return the length of the given string.
Limit the number of characters in a string.
Convert the given string to lower-case.
Limit the number of words in a string.
Convert GitHub flavored Markdown into HTML.
Convert inline Markdown into HTML.
Mask a portion of a string with a repeated character.
Get the string matching the given pattern.
Determine if a given string matches a given pattern.
Get the string matching the given pattern.
Remove all non-numeric characters from a string.
Pad both sides of a string with another.
Pad the left side of a string with another.
Pad the right side of a string with another.
Parse a Class[@]method style callback into class and method.
Pluralize the last word of an English, studly caps case string.
Pluralize the last word of an English, Pascal caps case string.
Generate a random, secure password.
Find the multi-byte safe position of the first occurrence of a given substring in a string.
Generate a more truly "random" alpha-numeric string.
Set the callable that will be used to generate random strings.
Set the sequence that will be used to generate random strings.
Indicate that random strings should be created normally and not using a custom factory.
Repeat the given string.
Replace a given value in the string sequentially with an array.
Replace the given value in the given string.
Replace the given value in the given string regardless of case.
Replace the first occurrence of a given value in the string.
Replace the first occurrence of the given value if it appears at the start of the string.
Replace the last occurrence of a given value in the string.
Replace the last occurrence of a given value if it appears at the end of the string.
Replace the patterns matching the given regular expression.
Remove any occurrence of the given string in the subject.
Reverse the given string.
Begin a string with a single instance of a given value.
Convert the given string to upper-case.
Convert the given string to proper case.
Convert the given string to proper case for each word.
Get the "initials" representing each word in the provided string, optionally capitalizing.
Convert the given string to APA-style title case.
Get the singular form of an English word.
Generate a URL friendly "slug" from a given string.
Convert a string to snake case.
Remove all whitespace from both ends of a string.
Remove all whitespace from the beginning of a string.
Remove all whitespace from the end of a string.
Remove all "extra" blank space from the given string.
Determine if a given string starts with a given substring.
Determine if a given string doesn't start with a given substring.
Convert a value to studly caps case.
Convert a value to Pascal case.
Return the portion of the string specified by the start and length parameters.
Return the number of substring occurrences.
Replace text within a portion of a string.
Swap multiple keywords in a string with other keywords.
Take the first or last {$limit} characters of a string.
Convert the given string to Base64 encoding.
Decode the given Base64 encoded string.
Make a string's first character lowercase.
Make a string's first character uppercase.
Capitalize the first character of each word in a string.
Split a string into pieces by uppercase characters.
Get the number of words a string contains.
Wrap a string to a given number of characters.
Generate a UUID (version 4).
Generate a time-ordered UUID (version 7).
Resolve a UUID from the custom factory, coercing strings to Uuid instances.
Set the callable that will be used to generate UUIDs.
Set the sequence that will be used to generate UUIDs.
Indicate that UUIDs should be created normally and not using a custom factory.
Indicate that ULIDs should be created normally and not using a custom factory.
Set the callable that will be used to generate ULIDs.
Set the sequence that will be used to generate ULIDs.
Remove all strings from the casing caches.
Return all factory functions to their default state.
Flush all static state.
Details
in
Macroable at line 30
static void
macro(string $name, callable|object $macro)
Register a custom macro.
Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.
in
Macroable at line 44
static void
mixin(object $mixin, bool $replace = true)
Mix another object into the class.
Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.
in
Macroable at line 61
static bool
hasMacro(string $name)
Check if macro is registered.
in
Macroable at line 72
static void
flushMacros()
Flush the existing macros.
Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.
in
Macroable at line 83
static mixed
__callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
in
Macroable at line 108
mixed
__call(string $method, array $parameters)
Dynamically handle calls to the class.
at line 63
static Stringable
of(mixed $string)
Get a new stringable object from the given string.
at line 71
static Stringable
trans(string $key, array $replace = [], string|null $locale = null)
Translate the given message and get a new stringable object.
at line 79
static string
after(string $subject, string|int|float|bool|Stringable|null $search)
Return the remainder of a string after the first occurrence of a given value.
at line 89
static string
afterLast(string $subject, string|int|float|bool|Stringable|null $search)
Return the remainder of a string after the last occurrence of a given value.
at line 109
static string
ascii(string|int|float|bool|Stringable|null $value, string $language = 'en')
Transliterate a UTF-8 value to ASCII.
at line 117
static string
transliterate(string|int|float|bool|Stringable|null $string, string|null $unknown = '?', bool $strict = false)
Transliterate a string to its closest ASCII representation.
at line 125
static string
before(string $subject, string|int|float|bool|Stringable|null $search)
Get the portion of a string before the first occurrence of a given value.
at line 141
static string
beforeLast(string $subject, string|int|float|bool|Stringable|null $search)
Get the portion of a string before the last occurrence of a given value.
at line 162
static string
between(string $subject, string|int|float|bool|Stringable|null $from, string|int|float|bool|Stringable|null $to)
Get the portion of a string between two given values.
at line 177
static string
betweenFirst(string $subject, string|int|float|bool|Stringable|null $from, string|int|float|bool|Stringable|null $to)
Get the smallest possible portion of a string between two given values.
at line 194
static string
camel(string $value)
Convert a value to camel case.
at line 202
static string|false
charAt(string $subject, int $index)
Get the character at the specified index.
at line 218
static string
chopStart(string $subject, string|array $needle)
Remove the given string(s) if it exists at the start of the haystack.
at line 234
static string
chopEnd(string $subject, string|array $needle)
Remove the given string(s) if it exists at the end of the haystack.
at line 256
static bool
contains(string $haystack, string|iterable $needles, bool $ignoreCase = false)
Determine if a given string contains a given substring.
at line 285
static bool
containsAll(string $haystack, iterable $needles, bool $ignoreCase = false)
Determine if a given string contains all array values.
at line 306
static bool
doesntContain(string $haystack, string|iterable $needles, bool $ignoreCase = false)
Determine if a given string doesn't contain a given substring.
at line 317
static string
convertCase(string $string, int $mode = MB_CASE_FOLD, string|null $encoding = 'UTF-8')
Convert the case of a string.
at line 325
static string
counted(string $value, int|array|Countable $count)
Get the plural form of an English word with the count prepended.
at line 336
static string
deduplicate(string $string, array|string $characters = ' ')
Replace consecutive instances of a given character with a single character in the given string.
at line 355
static bool
endsWith(string|int|float|bool|Stringable|null $haystack, string|int|float|bool|Stringable|iterable|null $needles)
Determine if a given string ends with a given substring.
at line 384
static bool
doesntEndWith(string|int|float|bool|Stringable|null $haystack, string|int|float|bool|Stringable|iterable|null $needles)
Determine if a given string doesn't end with a given substring.
at line 394
static string|null
excerpt(string|int|float|bool|Stringable|null $text, string|int|float|bool|Stringable|null $phrase = '', array $options = [])
Extract an excerpt from text that matches the first instance of a phrase.
at line 430
static string
finish(string $value, string $cap)
Cap a string with a single instance of a given value.
at line 454
static string
wrap(string $value, string $before, string|null $after = null)
Wrap the string with the given strings.
at line 462
static string
unwrap(string $value, string $before, string|null $after = null)
Unwrap the string with the given strings.
at line 485
static bool
is(string|int|float|bool|Stringable|iterable|null $pattern, string|int|float|bool|Stringable|null $value, bool $ignoreCase = false)
Determine if a given string matches a given pattern.
at line 525
static bool
isAscii(string|int|float|bool|Stringable|null $value)
Determine if a given string is 7 bit ASCII.
at line 535
static bool
isJson(mixed $value)
Determine if a given value is valid JSON.
at line 551
static bool
isUrl(mixed $value, array $protocols = [])
Determine if a given value is a valid URL.
at line 605
static bool
isUuid(mixed $value, int|string|null $version = null)
Determine if a given value is a valid UUID.
at line 635
static bool
isUlid(mixed $value)
Determine if a given value is a valid ULID.
at line 649
static string
kebab(string $value)
Convert a string to kebab case.
at line 659
static int
length(string $value, string|null $encoding = null)
Return the length of the given string.
at line 667
static string
limit(string $value, int $limit = 100, string $end = '...', bool $preserveWords = false)
Limit the number of characters in a string.
at line 693
static string
lower(string $value)
Convert the given string to lower-case.
at line 701
static string
words(string $value, int $words = 100, string $end = '...')
Limit the number of words in a string.
at line 718
static string
markdown(string|int|float|bool|Stringable|null $string, array $options = [], array $extensions = [])
Convert GitHub flavored Markdown into HTML.
at line 739
static string
inlineMarkdown(string|int|float|bool|Stringable|null $string, array $options = [], array $extensions = [])
Convert inline Markdown into HTML.
at line 760
static string
mask(string $string, string|Stringable $character, int $index, int|null $length = null, string $encoding = 'UTF-8')
Mask a portion of a string with a repeated character.
at line 791
static string
match(string $pattern, string $subject)
Get the string matching the given pattern.
at line 808
static bool
isMatch(string|iterable $pattern, string $value)
Determine if a given string matches a given pattern.
at line 828
static Collection
matchAll(string $pattern, string $subject)
Get the string matching the given pattern.
at line 845
static string|array
numbers(string|array $value)
Remove all non-numeric characters from a string.
at line 853
static string
padBoth(string $value, int $length, string $pad = ' ')
Pad both sides of a string with another.
at line 861
static string
padLeft(string $value, int $length, string $pad = ' ')
Pad the left side of a string with another.
at line 869
static string
padRight(string $value, int $length, string $pad = ' ')
Pad the right side of a string with another.
at line 879
static array
parseCallback(string $callback, string|null $default = null)
Parse a Class[@]method style callback into class and method.
at line 898
static string
plural(string $value, int|array|Countable $count = 2, bool $prependCount = false)
Get the plural form of an English word.
at line 910
static string
pluralStudly(string $value, int|array|Countable $count = 2)
Pluralize the last word of an English, studly caps case string.
at line 922
static string
pluralPascal(string $value, int|array|Countable $count = 2)
Pluralize the last word of an English, Pascal caps case string.
at line 932
static string
password(int $length = 32, bool $letters = true, bool $numbers = true, bool $symbols = true, bool $spaces = false)
Generate a random, secure password.
at line 978
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.
at line 986
static string
random(int $length = 16)
Generate a more truly "random" alpha-numeric string.
at line 1013
static void
createRandomStringsUsing(callable|null $factory = null)
Set the callable that will be used to generate random strings.
The factory persists in a static property for the worker lifetime and affects every subsequent random string generation.
at line 1028
static void
createRandomStringsUsingSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to generate random strings.
The sequence factory persists in a static property for the worker lifetime and affects every subsequent random string generation.
at line 1064
static void
createRandomStringsNormally()
Indicate that random strings should be created normally and not using a custom factory.
Clears the worker-wide random string factory; concurrent coroutines may observe different random string behavior depending on timing.
at line 1073
static string
repeat(string $string, int $times)
Repeat the given string.
at line 1083
static string
replaceArray(string $search, iterable $replace, string $subject)
Replace a given value in the string sequentially with an array.
at line 1128
static string|array
replace(string|iterable $search, string|iterable $replace, string|iterable $subject, bool $caseSensitive = true)
Replace the given value in the given string.
at line 1157
static protected string|array
replaceWhileIgnoringCase(string|array $search, string|array $replace, string|array $subject)
Replace the given value in the given string regardless of case.
at line 1203
static string
replaceFirst(string|int|float|bool|Stringable|null $search, string $replace, string $subject)
Replace the first occurrence of a given value in the string.
at line 1223
static string
replaceStart(string|int|float|bool|Stringable|null $search, string $replace, string $subject)
Replace the first occurrence of the given value if it appears at the start of the string.
at line 1241
static string
replaceLast(string $search, string $replace, string $subject)
Replace the last occurrence of a given value in the string.
at line 1259
static string
replaceEnd(string|int|float|bool|Stringable|null $search, string $replace, string $subject)
Replace the last occurrence of a given value if it appears at the end of the string.
at line 1282
static string|array|null
replaceMatches(string|array $pattern, Closure|array|string $replace, string|array $subject, int $limit = -1)
Replace the patterns matching the given regular expression.
at line 1298
static string|array
remove(string|iterable $search, string|iterable $subject, bool $caseSensitive = true)
Remove any occurrence of the given string in the subject.
at line 1316
static string
reverse(string $value)
Reverse the given string.
at line 1326
static string
start(string $value, string $prefix)
Begin a string with a single instance of a given value.
at line 1350
static string
upper(string $value)
Convert the given string to upper-case.
at line 1358
static string
title(string $value)
Convert the given string to proper case.
at line 1366
static string
headline(string $value)
Convert the given string to proper case for each word.
at line 1382
static string
initials(string $value, bool $capitalize = false)
Get the "initials" representing each word in the provided string, optionally capitalizing.
at line 1398
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
at line 1449
static string
singular(string $value)
Get the singular form of an English word.
at line 1459
static string
slug(string|int|float|bool|Stringable|null $title, string $separator = '-', string|null $language = 'en', array $dictionary = ['@' => 'at'])
Generate a URL friendly "slug" from a given string.
at line 1489
static string
snake(string $value, string $delimiter = '_')
Convert a string to snake case.
at line 1503
static string
trim(string $value, string|null $charlist = null)
Remove all whitespace from both ends of a string.
at line 1535
static string
ltrim(string $value, string|null $charlist = null)
Remove all whitespace from the beginning of a string.
at line 1549
static string
rtrim(string $value, string|null $charlist = null)
Remove all whitespace from the end of a string.
at line 1566
static string
squish(string $value)
Remove all "extra" blank space from the given string.
at line 1579
static bool
startsWith(string|int|float|bool|Stringable|null $haystack, string|int|float|bool|Stringable|iterable|null $needles)
Determine if a given string starts with a given substring.
at line 1610
static bool
doesntStartWith(string|int|float|bool|Stringable|null $haystack, string|int|float|bool|Stringable|iterable|null $needles)
Determine if a given string doesn't start with a given substring.
at line 1621
static string
studly(string $value, bool $normalize = false)
Convert a value to studly caps case.
at line 1644
static string
pascal(string $value, bool $normalize = false)
Convert a value to Pascal case.
at line 1652
static string
substr(string $string, int $start, int|null $length = null, string $encoding = 'UTF-8')
Return the portion of the string specified by the start and length parameters.
at line 1660
static int
substrCount(string $haystack, string $needle, int $offset = 0, int|null $length = null)
Return the number of substring occurrences.
at line 1674
static string|array
substrReplace(string|array $string, string|array $replace, int|array $offset = 0, int|array|null $length = null)
Replace text within a portion of a string.
at line 1726
static string
swap(array $map, string $subject)
Swap multiple keywords in a string with other keywords.
at line 1734
static string
take(string $string, int $limit)
Take the first or last {$limit} characters of a string.
at line 1748
static string
toBase64(string $string)
Convert the given string to Base64 encoding.
at line 1758
static string|false
fromBase64(string $string, bool $strict = false)
Decode the given Base64 encoded string.
at line 1768
static string
lcfirst(string $string)
Make a string's first character lowercase.
at line 1778
static string
ucfirst(string $string)
Make a string's first character uppercase.
at line 1788
static string
ucwords(string $string, string $separators = " \t\r\n\f\v")
Capitalize the first character of each word in a string.
at line 1806
static array
ucsplit(string $string)
Split a string into pieces by uppercase characters.
at line 1816
static int
wordCount(string $string, string|null $characters = null)
Get the number of words a string contains.
at line 1824
static string
wordWrap(string $string, int $characters = 75, string $break = "\n", bool $cutLongWords = false)
Wrap a string to a given number of characters.
at line 1858
static Uuid
uuid()
Generate a UUID (version 4).
at line 1868
static Uuid
uuid7(DateTimeInterface|null $time = null)
Generate a UUID (version 7).
at line 1884
static Uuid
orderedUuid()
Generate a time-ordered UUID (version 7).
at line 1894
static protected Uuid
factoryUuid()
Resolve a UUID from the custom factory, coercing strings to Uuid instances.
at line 1909
static void
createUuidsUsing(callable|null $factory = null)
Set the callable that will be used to generate UUIDs.
The factory persists in a static property for the worker lifetime and affects every subsequent UUID generation.
at line 1924
static void
createUuidsUsingSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to generate UUIDs.
The sequence factory persists in a static property for the worker lifetime and affects every subsequent UUID generation.
at line 1962
static Uuid
freezeUuids(Closure|null $callback = null)
Always return the same UUID when generating new UUIDs.
The factory persists in a static property and affects every UUID the worker generates, until the callback returns when one is supplied.
at line 1986
static void
createUuidsNormally()
Indicate that UUIDs should be created normally and not using a custom factory.
Clears the worker-wide UUID factory; concurrent coroutines may observe different UUID behavior depending on timing.
at line 1995
static Ulid
ulid(DateTimeInterface|null $time = null)
Generate a ULID.
at line 2014
static void
createUlidsNormally()
Indicate that ULIDs should be created normally and not using a custom factory.
Clears the worker-wide ULID factory; concurrent coroutines may observe different ULID behavior depending on timing.
at line 2028
static void
createUlidsUsing(callable|null $factory = null)
Set the callable that will be used to generate ULIDs.
The factory persists in a static property for the worker lifetime and affects every subsequent ULID generation.
at line 2043
static void
createUlidsUsingSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to generate ULIDs.
The sequence factory persists in a static property for the worker lifetime and affects every subsequent ULID generation.
at line 2081
static Ulid
freezeUlids(Closure|null $callback = null)
Always return the same ULID when generating new ULIDs.
The factory persists in a static property and affects every ULID the worker generates, until the callback returns when one is supplied.
at line 2102
static void
flushCache()
Remove all strings from the casing caches.
at line 2114
static void
resetFactoryState()
Return all factory functions to their default state.
Clears the worker-wide random string, UUID, and ULID factories, affecting generation in every coroutine.
at line 2125
static void
flushState()
Flush all static state.