class Stringable implements JsonSerializable (View source)

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected string $value

The underlying string value.

Methods

$this|TWhenReturnType
when($value = null, callable $callback = null, callable $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

$this|TUnlessReturnType
unless($value = null, callable $callback = null, callable $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

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.

tap(callable|null $callback = null)

Call the given Closure with this instance then return the instance.

from  Tappable
void
__construct(string $value = '')

Create a new instance of the class.

after(string $search)

Return the remainder of a string after the first occurrence of a given value.

afterLast(string $search)

Return the remainder of a string after the last occurrence of a given value.

append(string ...$values)

Append the given values to the string.

$this
newLine(int $count = 1)

Append a new line to the string.

ascii(string $language = 'en')

Transliterate a UTF-8 value to ASCII.

basename(string $suffix = '')

Get the trailing name component of the path.

classBasename()

Get the basename of the class path.

before(string $search)

Get the portion of a string before the first occurrence of a given value.

beforeLast(string $search)

Get the portion of a string before the last occurrence of a given value.

between(string $from, string $to)

Get the portion of a string between two given values.

betweenFirst(string $from, string $to)

Get the smallest possible portion of a string between two given values.

camel()

Convert a value to camel case.

bool
contains(string|iterable<string> $needles, bool $ignoreCase = false)

Determine if a given string contains a given substring.

bool
containsAll(iterable<string> $needles, bool $ignoreCase = false)

Determine if a given string contains all array values.

dirname(int $levels = 1)

Get the parent directory's path.

bool
endsWith(string|iterable<string> $needles)

Determine if a given string ends with a given substring.

bool
exactly(Stringable|string $value)

Determine if the string is an exact match with the given value.

string|null
excerpt(string $phrase = '', array $options = [])

Extracts an excerpt from text that matches the first instance of a phrase.

explode(string $delimiter, int $limit = PHP_INT_MAX)

Explode the string into an array.

split(string|int $pattern, int $limit = -1, int $flags = 0)

Split a string using a regular expression or by length.

finish(string $cap)

Cap a string with a single instance of a given value.

bool
is(string|iterable<string> $pattern)

Determine if a given string matches a given pattern.

bool
isAscii()

Determine if a given string is 7 bit ASCII.

bool
isJson()

Determine if a given string is valid JSON.

bool
isUuid()

Determine if a given string is a valid UUID.

bool
isUlid()

Determine if a given string is a valid ULID.

bool
isEmpty()

Determine if the given string is empty.

bool
isNotEmpty()

Determine if the given string is not empty.

kebab()

Convert a string to kebab case.

int
length(string|null $encoding = null)

Return the length of the given string.

limit(int $limit = 100, string $end = '...')

Limit the number of characters in a string.

lower()

Convert the given string to lower-case.

markdown(array $options = [])

Convert GitHub flavored Markdown into HTML.

inlineMarkdown(array $options = [])

Convert inline Markdown into HTML.

mask(string $character, int $index, int|null $length = null, string $encoding = 'UTF-8')

Masks a portion of a string with a repeated character.

match(string $pattern)

Get the string matching the given pattern.

matchAll(string $pattern)

Get the string matching the given pattern.

bool
test(string $pattern)

Determine if the string matches the given pattern.

padBoth(int $length, string $pad = ' ')

Pad both sides of the string with another.

padLeft(int $length, string $pad = ' ')

Pad the left side of the string with another.

padRight(int $length, string $pad = ' ')

Pad the right side of the string with another.

array<int,string|null>
parseCallback(string|null $default = null)

Parse a Class@method style callback into class and method.

pipe(callable $callback)

Call the given callback and return a new string.

plural(int|array|Countable $count = 2)

Get the plural form of an English word.

pluralStudly(int|array|Countable $count = 2)

Pluralize the last word of an English, studly caps case string.

prepend(string ...$values)

Prepend the given values to the string.

remove(string|iterable<string> $search, bool $caseSensitive = true)

Remove any occurrence of the given string in the subject.

reverse()

Reverse the string.

repeat(int $times)

Repeat the string.

replace(string|iterable<string> $search, string|iterable<string> $replace)

Replace the given value in the given string.

replaceArray(string $search, iterable<string> $replace)

Replace a given value in the string sequentially with an array.

replaceFirst(string $search, string $replace)

Replace the first occurrence of a given value in the string.

replaceLast(string $search, string $replace)

Replace the last occurrence of a given value in the string.

replaceMatches(string $pattern, Closure|string $replace, int $limit = -1)

Replace the patterns matching the given regular expression.

scan(string $format)

Parse input from a string to a collection, according to a format.

squish()

Remove all "extra" blank space from the given string.

start(string $prefix)

Begin a string with a single instance of a given value.

stripTags(string $allowedTags = null)

Strip HTML and PHP tags from the given string.

upper()

Convert the given string to upper-case.

title()

Convert the given string to title case.

headline()

Convert the given string to title case for each word.

singular()

Get the singular form of an English word.

slug(string $separator = '-', string|null $language = 'en', array<string,string> $dictionary = ['@' => 'at'])

Generate a URL friendly "slug" from a given string.

snake(string $delimiter = '_')

Convert a string to snake case.

bool
startsWith(string|iterable<string> $needles)

Determine if a given string starts with a given substring.

studly()

Convert a value to studly caps case.

substr(int $start, int|null $length = null, string $encoding = 'UTF-8')

Returns the portion of the string specified by the start and length parameters.

int
substrCount(string $needle, int $offset = 0, int|null $length = null)

Returns the number of substring occurrences.

substrReplace(string|string[] $replace, int|int[] $offset = 0, int|int[]|null $length = null)

Replace text within a portion of a string.

swap(array $map)

Swap multiple keywords in a string with other keywords.

trim(string $characters = null)

Trim the string of the given characters.

ltrim(string $characters = null)

Left trim the string of the given characters.

rtrim(string $characters = null)

Right trim the string of the given characters.

lcfirst()

Make a string's first character lowercase.

ucfirst()

Make a string's first character uppercase.

ucsplit()

Split a string by uppercase characters.

whenContains(string|iterable<string> $needles, callable $callback, callable|null $default = null)

Execute the given callback if the string contains a given substring.

whenContainsAll(array $needles, callable $callback, callable|null $default = null)

Execute the given callback if the string contains all array values.

whenEmpty(callable $callback, callable|null $default = null)

Execute the given callback if the string is empty.

whenNotEmpty(callable $callback, callable|null $default = null)

Execute the given callback if the string is not empty.

whenEndsWith(string|iterable<string> $needles, callable $callback, callable|null $default = null)

Execute the given callback if the string ends with a given substring.

whenExactly(string $value, callable $callback, callable|null $default = null)

Execute the given callback if the string is an exact match with the given value.

whenNotExactly(string $value, callable $callback, callable|null $default = null)

Execute the given callback if the string is not an exact match with the given value.

whenIs(string|iterable<string> $pattern, callable $callback, callable|null $default = null)

Execute the given callback if the string matches a given pattern.

whenIsAscii(callable $callback, callable|null $default = null)

Execute the given callback if the string is 7 bit ASCII.

whenIsUuid(callable $callback, callable|null $default = null)

Execute the given callback if the string is a valid UUID.

whenIsUlid(callable $callback, callable|null $default = null)

Execute the given callback if the string is a valid ULID.

whenStartsWith(string|iterable<string> $needles, callable $callback, callable|null $default = null)

Execute the given callback if the string starts with a given substring.

whenTest(string $pattern, callable $callback, callable|null $default = null)

Execute the given callback if the string matches the given pattern.

words(int $words = 100, string $end = '...')

Limit the number of words in a string.

int
wordCount(string|null $characters = null)

Get the number of words a string contains.

wrap(string $before, string|null $after = null)

Wrap the string with the given strings.

toHtmlString()

Convert the string into a HtmlString instance.

$this
dump()

Dump the string.

never
dd()

Dump the string and end the script.

string
value()

Get the underlying string value.

string
toString()

Get the underlying string value.

int
toInteger()

Get the underlying string value as an integer.

float
toFloat()

Get the underlying string value as a float.

bool
toBoolean()

Get the underlying string value as a boolean.

toDate(string|null $format = null, string|null $tz = null)

Get the underlying string value as a Carbon instance.

string
jsonSerialize()

Convert the object to a string when JSON encoded.

mixed
__get(string $key)

Proxy dynamic properties onto methods.

string
__toString()

Get the raw string value.

Details

$this|TWhenReturnType when($value = null, callable $callback = null, callable $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

Parameters

$value
callable $callback
callable $default

Return Value

$this|TWhenReturnType

$this|TUnlessReturnType unless($value = null, callable $callback = null, callable $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

Parameters

$value
callable $callback
callable $default

Return Value

$this|TUnlessReturnType

static void macro(string $name, object|callable $macro)

Register a custom macro.

Parameters

string $name
object|callable $macro

Return Value

void

static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

static void flushMacros()

Flush the existing macros.

Return Value

void

static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

$this|HigherOrderTapProxy tap(callable|null $callback = null)

Call the given Closure with this instance then return the instance.

Parameters

callable|null $callback

Return Value

$this|HigherOrderTapProxy

void __construct(string $value = '')

Create a new instance of the class.

Parameters

string $value

Return Value

void

Stringable after(string $search)

Return the remainder of a string after the first occurrence of a given value.

Parameters

string $search

Return Value

Stringable

Stringable afterLast(string $search)

Return the remainder of a string after the last occurrence of a given value.

Parameters

string $search

Return Value

Stringable

Stringable append(string ...$values)

Append the given values to the string.

Parameters

string ...$values

Return Value

Stringable

$this newLine(int $count = 1)

Append a new line to the string.

Parameters

int $count

Return Value

$this

Stringable ascii(string $language = 'en')

Transliterate a UTF-8 value to ASCII.

Parameters

string $language

Return Value

Stringable

Stringable basename(string $suffix = '')

Get the trailing name component of the path.

Parameters

string $suffix

Return Value

Stringable

Stringable classBasename()

Get the basename of the class path.

Return Value

Stringable

Stringable before(string $search)

Get the portion of a string before the first occurrence of a given value.

Parameters

string $search

Return Value

Stringable

Stringable beforeLast(string $search)

Get the portion of a string before the last occurrence of a given value.

Parameters

string $search

Return Value

Stringable

Stringable between(string $from, string $to)

Get the portion of a string between two given values.

Parameters

string $from
string $to

Return Value

Stringable

Stringable betweenFirst(string $from, string $to)

Get the smallest possible portion of a string between two given values.

Parameters

string $from
string $to

Return Value

Stringable

Stringable camel()

Convert a value to camel case.

Return Value

Stringable

bool contains(string|iterable<string> $needles, bool $ignoreCase = false)

Determine if a given string contains a given substring.

Parameters

string|iterable<string> $needles
bool $ignoreCase

Return Value

bool

bool containsAll(iterable<string> $needles, bool $ignoreCase = false)

Determine if a given string contains all array values.

Parameters

iterable<string> $needles
bool $ignoreCase

Return Value

bool

Stringable dirname(int $levels = 1)

Get the parent directory's path.

Parameters

int $levels

Return Value

Stringable

bool endsWith(string|iterable<string> $needles)

Determine if a given string ends with a given substring.

Parameters

string|iterable<string> $needles

Return Value

bool

bool exactly(Stringable|string $value)

Determine if the string is an exact match with the given value.

Parameters

Stringable|string $value

Return Value

bool

string|null excerpt(string $phrase = '', array $options = [])

Extracts an excerpt from text that matches the first instance of a phrase.

Parameters

string $phrase
array $options

Return Value

string|null

Collection explode(string $delimiter, int $limit = PHP_INT_MAX)

Explode the string into an array.

Parameters

string $delimiter
int $limit

Return Value

Collection

Collection split(string|int $pattern, int $limit = -1, int $flags = 0)

Split a string using a regular expression or by length.

Parameters

string|int $pattern
int $limit
int $flags

Return Value

Collection

Stringable finish(string $cap)

Cap a string with a single instance of a given value.

Parameters

string $cap

Return Value

Stringable

bool is(string|iterable<string> $pattern)

Determine if a given string matches a given pattern.

Parameters

string|iterable<string> $pattern

Return Value

bool

bool isAscii()

Determine if a given string is 7 bit ASCII.

Return Value

bool

bool isJson()

Determine if a given string is valid JSON.

Return Value

bool

bool isUuid()

Determine if a given string is a valid UUID.

Return Value

bool

bool isUlid()

Determine if a given string is a valid ULID.

Return Value

bool

bool isEmpty()

Determine if the given string is empty.

Return Value

bool

bool isNotEmpty()

Determine if the given string is not empty.

Return Value

bool

Stringable kebab()

Convert a string to kebab case.

Return Value

Stringable

int length(string|null $encoding = null)

Return the length of the given string.

Parameters

string|null $encoding

Return Value

int

Stringable limit(int $limit = 100, string $end = '...')

Limit the number of characters in a string.

Parameters

int $limit
string $end

Return Value

Stringable

Stringable lower()

Convert the given string to lower-case.

Return Value

Stringable

Stringable markdown(array $options = [])

Convert GitHub flavored Markdown into HTML.

Parameters

array $options

Return Value

Stringable

Stringable inlineMarkdown(array $options = [])

Convert inline Markdown into HTML.

Parameters

array $options

Return Value

Stringable

Stringable mask(string $character, int $index, int|null $length = null, string $encoding = 'UTF-8')

Masks a portion of a string with a repeated character.

Parameters

string $character
int $index
int|null $length
string $encoding

Return Value

Stringable

Stringable match(string $pattern)

Get the string matching the given pattern.

Parameters

string $pattern

Return Value

Stringable

Collection matchAll(string $pattern)

Get the string matching the given pattern.

Parameters

string $pattern

Return Value

Collection

bool test(string $pattern)

Determine if the string matches the given pattern.

Parameters

string $pattern

Return Value

bool

Stringable padBoth(int $length, string $pad = ' ')

Pad both sides of the string with another.

Parameters

int $length
string $pad

Return Value

Stringable

Stringable padLeft(int $length, string $pad = ' ')

Pad the left side of the string with another.

Parameters

int $length
string $pad

Return Value

Stringable

Stringable padRight(int $length, string $pad = ' ')

Pad the right side of the string with another.

Parameters

int $length
string $pad

Return Value

Stringable

array<int,string|null> parseCallback(string|null $default = null)

Parse a Class@method style callback into class and method.

Parameters

string|null $default

Return Value

array<int,string|null>

Stringable pipe(callable $callback)

Call the given callback and return a new string.

Parameters

callable $callback

Return Value

Stringable

Stringable plural(int|array|Countable $count = 2)

Get the plural form of an English word.

Parameters

int|array|Countable $count

Return Value

Stringable

Stringable pluralStudly(int|array|Countable $count = 2)

Pluralize the last word of an English, studly caps case string.

Parameters

int|array|Countable $count

Return Value

Stringable

Stringable prepend(string ...$values)

Prepend the given values to the string.

Parameters

string ...$values

Return Value

Stringable

Stringable remove(string|iterable<string> $search, bool $caseSensitive = true)

Remove any occurrence of the given string in the subject.

Parameters

string|iterable<string> $search
bool $caseSensitive

Return Value

Stringable

Stringable reverse()

Reverse the string.

Return Value

Stringable

Stringable repeat(int $times)

Repeat the string.

Parameters

int $times

Return Value

Stringable

Stringable replace(string|iterable<string> $search, string|iterable<string> $replace)

Replace the given value in the given string.

Parameters

string|iterable<string> $search
string|iterable<string> $replace

Return Value

Stringable

Stringable replaceArray(string $search, iterable<string> $replace)

Replace a given value in the string sequentially with an array.

Parameters

string $search
iterable<string> $replace

Return Value

Stringable

Stringable replaceFirst(string $search, string $replace)

Replace the first occurrence of a given value in the string.

Parameters

string $search
string $replace

Return Value

Stringable

Stringable replaceLast(string $search, string $replace)

Replace the last occurrence of a given value in the string.

Parameters

string $search
string $replace

Return Value

Stringable

Stringable replaceMatches(string $pattern, Closure|string $replace, int $limit = -1)

Replace the patterns matching the given regular expression.

Parameters

string $pattern
Closure|string $replace
int $limit

Return Value

Stringable

Collection scan(string $format)

Parse input from a string to a collection, according to a format.

Parameters

string $format

Return Value

Collection

Stringable squish()

Remove all "extra" blank space from the given string.

Return Value

Stringable

Stringable start(string $prefix)

Begin a string with a single instance of a given value.

Parameters

string $prefix

Return Value

Stringable

Stringable stripTags(string $allowedTags = null)

Strip HTML and PHP tags from the given string.

Parameters

string $allowedTags

Return Value

Stringable

Stringable upper()

Convert the given string to upper-case.

Return Value

Stringable

Stringable title()

Convert the given string to title case.

Return Value

Stringable

Stringable headline()

Convert the given string to title case for each word.

Return Value

Stringable

Stringable singular()

Get the singular form of an English word.

Return Value

Stringable

Stringable slug(string $separator = '-', string|null $language = 'en', array<string,string> $dictionary = ['@' => 'at'])

Generate a URL friendly "slug" from a given string.

Parameters

string $separator
string|null $language
array<string,string> $dictionary

Return Value

Stringable

Stringable snake(string $delimiter = '_')

Convert a string to snake case.

Parameters

string $delimiter

Return Value

Stringable

bool startsWith(string|iterable<string> $needles)

Determine if a given string starts with a given substring.

Parameters

string|iterable<string> $needles

Return Value

bool

Stringable studly()

Convert a value to studly caps case.

Return Value

Stringable

Stringable substr(int $start, int|null $length = null, string $encoding = 'UTF-8')

Returns the portion of the string specified by the start and length parameters.

Parameters

int $start
int|null $length
string $encoding

Return Value

Stringable

int substrCount(string $needle, int $offset = 0, int|null $length = null)

Returns the number of substring occurrences.

Parameters

string $needle
int $offset
int|null $length

Return Value

int

Stringable substrReplace(string|string[] $replace, int|int[] $offset = 0, int|int[]|null $length = null)

Replace text within a portion of a string.

Parameters

string|string[] $replace
int|int[] $offset
int|int[]|null $length

Return Value

Stringable

Stringable swap(array $map)

Swap multiple keywords in a string with other keywords.

Parameters

array $map

Return Value

Stringable

Stringable trim(string $characters = null)

Trim the string of the given characters.

Parameters

string $characters

Return Value

Stringable

Stringable ltrim(string $characters = null)

Left trim the string of the given characters.

Parameters

string $characters

Return Value

Stringable

Stringable rtrim(string $characters = null)

Right trim the string of the given characters.

Parameters

string $characters

Return Value

Stringable

Stringable lcfirst()

Make a string's first character lowercase.

Return Value

Stringable

Stringable ucfirst()

Make a string's first character uppercase.

Return Value

Stringable

Collection ucsplit()

Split a string by uppercase characters.

Return Value

Collection

Stringable whenContains(string|iterable<string> $needles, callable $callback, callable|null $default = null)

Execute the given callback if the string contains a given substring.

Parameters

string|iterable<string> $needles
callable $callback
callable|null $default

Return Value

Stringable

Stringable whenContainsAll(array $needles, callable $callback, callable|null $default = null)

Execute the given callback if the string contains all array values.

Parameters

array $needles
callable $callback
callable|null $default

Return Value

Stringable

Stringable whenEmpty(callable $callback, callable|null $default = null)

Execute the given callback if the string is empty.

Parameters

callable $callback
callable|null $default

Return Value

Stringable

Stringable whenNotEmpty(callable $callback, callable|null $default = null)

Execute the given callback if the string is not empty.

Parameters

callable $callback
callable|null $default

Return Value

Stringable

Stringable whenEndsWith(string|iterable<string> $needles, callable $callback, callable|null $default = null)

Execute the given callback if the string ends with a given substring.

Parameters

string|iterable<string> $needles
callable $callback
callable|null $default

Return Value

Stringable

Stringable whenExactly(string $value, callable $callback, callable|null $default = null)

Execute the given callback if the string is an exact match with the given value.

Parameters

string $value
callable $callback
callable|null $default

Return Value

Stringable

Stringable whenNotExactly(string $value, callable $callback, callable|null $default = null)

Execute the given callback if the string is not an exact match with the given value.

Parameters

string $value
callable $callback
callable|null $default

Return Value

Stringable

Stringable whenIs(string|iterable<string> $pattern, callable $callback, callable|null $default = null)

Execute the given callback if the string matches a given pattern.

Parameters

string|iterable<string> $pattern
callable $callback
callable|null $default

Return Value

Stringable

Stringable whenIsAscii(callable $callback, callable|null $default = null)

Execute the given callback if the string is 7 bit ASCII.

Parameters

callable $callback
callable|null $default

Return Value

Stringable

Stringable whenIsUuid(callable $callback, callable|null $default = null)

Execute the given callback if the string is a valid UUID.

Parameters

callable $callback
callable|null $default

Return Value

Stringable

Stringable whenIsUlid(callable $callback, callable|null $default = null)

Execute the given callback if the string is a valid ULID.

Parameters

callable $callback
callable|null $default

Return Value

Stringable

Stringable whenStartsWith(string|iterable<string> $needles, callable $callback, callable|null $default = null)

Execute the given callback if the string starts with a given substring.

Parameters

string|iterable<string> $needles
callable $callback
callable|null $default

Return Value

Stringable

Stringable whenTest(string $pattern, callable $callback, callable|null $default = null)

Execute the given callback if the string matches the given pattern.

Parameters

string $pattern
callable $callback
callable|null $default

Return Value

Stringable

Stringable words(int $words = 100, string $end = '...')

Limit the number of words in a string.

Parameters

int $words
string $end

Return Value

Stringable

int wordCount(string|null $characters = null)

Get the number of words a string contains.

Parameters

string|null $characters

Return Value

int

Stringable wrap(string $before, string|null $after = null)

Wrap the string with the given strings.

Parameters

string $before
string|null $after

Return Value

Stringable

HtmlString toHtmlString()

Convert the string into a HtmlString instance.

Return Value

HtmlString

$this dump()

Dump the string.

Return Value

$this

never dd()

Dump the string and end the script.

Return Value

never

string value()

Get the underlying string value.

Return Value

string

string toString()

Get the underlying string value.

Return Value

string

int toInteger()

Get the underlying string value as an integer.

Return Value

int

float toFloat()

Get the underlying string value as a float.

Return Value

float

bool toBoolean()

Get the underlying string value as a boolean.

Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.

Return Value

bool

Carbon toDate(string|null $format = null, string|null $tz = null)

Get the underlying string value as a Carbon instance.

Parameters

string|null $format
string|null $tz

Return Value

Carbon

Exceptions

InvalidFormatException

string jsonSerialize()

Convert the object to a string when JSON encoded.

Return Value

string

mixed __get(string $key)

Proxy dynamic properties onto methods.

Parameters

string $key

Return Value

mixed

string __toString()

Get the raw string value.

Return Value

string