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(bool $value, callable $callback = null, callable $default = null)

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

$this|TUnlessReturnType
unless(bool $value, 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(array ...$values)

Append the given values 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.

camel()

Convert a value to camel case.

bool
contains(string|array $needles)

Determine if a given string contains a given substring.

bool
containsAll(array $needles)

Determine if a given string contains all array values.

dirname(int $levels = 1)

Get the parent directory's path.

bool
endsWith(string|array $needles)

Determine if a given string ends with a given substring.

bool
exactly(string $value)

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

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|array $pattern)

Determine if a given string matches a given pattern.

bool
isAscii()

Determine if a given string is 7 bit ASCII.

bool
isUuid()

Determine if a given string is a valid UUID.

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 $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.

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
parseCallback(string|null $default = null)

Parse a [email protected] style callback into class and method.

pipe(callable $callback)

Call the given callback and return a new string.

plural(int $count = 2)

Get the plural form of an English word.

pluralStudly(int $count = 2)

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

prepend(array ...$values)

Prepend the given values to the string.

remove(string|string[] $search, bool $caseSensitive = true)

Remove any occurrence of the given string in the subject.

repeat(int $times)

Repeat the string.

replace(string|string[] $search, string|string[] $replace)

Replace the given value in the given string.

replaceArray(string $search, array $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.

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')

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

snake(string $delimiter = '_')

Convert a string to snake case.

bool
startsWith(string|array $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)

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

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

Returns the number of substring occurrences.

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.

ucfirst()

Make a string's first character uppercase.

whenEmpty(callable $callback)

Execute the given callback if the string is empty.

whenNotEmpty(callable $callback)

Execute the given callback if the string is not empty.

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

Limit the number of words in a string.

int
wordCount()

Get the number of words a string contains.

$this
dump()

Dump the string.

never
dd()

Dump the string and end the script.

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(bool $value, callable $callback = null, callable $default = null)

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

Parameters

bool $value
callable $callback
callable $default

Return Value

$this|TWhenReturnType

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

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

Parameters

bool $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(array ...$values)

Append the given values to the string.

Parameters

array ...$values

Return Value

Stringable

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 camel()

Convert a value to camel case.

Return Value

Stringable

bool contains(string|array $needles)

Determine if a given string contains a given substring.

Parameters

string|array $needles

Return Value

bool

bool containsAll(array $needles)

Determine if a given string contains all array values.

Parameters

array $needles

Return Value

bool

Stringable dirname(int $levels = 1)

Get the parent directory's path.

Parameters

int $levels

Return Value

Stringable

bool endsWith(string|array $needles)

Determine if a given string ends with a given substring.

Parameters

string|array $needles

Return Value

bool

bool exactly(string $value)

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

Parameters

string $value

Return Value

bool

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|array $pattern)

Determine if a given string matches a given pattern.

Parameters

string|array $pattern

Return Value

bool

bool isAscii()

Determine if a given string is 7 bit ASCII.

Return Value

bool

bool isUuid()

Determine if a given string is a valid UUID.

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 $encoding = null)

Return the length of the given string.

Parameters

string $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 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 parseCallback(string|null $default = null)

Parse a [email protected] style callback into class and method.

Parameters

string|null $default

Return Value

array

Stringable pipe(callable $callback)

Call the given callback and return a new string.

Parameters

callable $callback

Return Value

Stringable

Stringable plural(int $count = 2)

Get the plural form of an English word.

Parameters

int $count

Return Value

Stringable

Stringable pluralStudly(int $count = 2)

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

Parameters

int $count

Return Value

Stringable

Stringable prepend(array ...$values)

Prepend the given values to the string.

Parameters

array ...$values

Return Value

Stringable

Stringable remove(string|string[] $search, bool $caseSensitive = true)

Remove any occurrence of the given string in the subject.

Parameters

string|string[] $search
bool $caseSensitive

Return Value

Stringable

Stringable repeat(int $times)

Repeat the string.

Parameters

int $times

Return Value

Stringable

Stringable replace(string|string[] $search, string|string[] $replace)

Replace the given value in the given string.

Parameters

string|string[] $search
string|string[] $replace

Return Value

Stringable

Stringable replaceArray(string $search, array $replace)

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

Parameters

string $search
array $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

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')

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

Parameters

string $separator
string|null $language

Return Value

Stringable

Stringable snake(string $delimiter = '_')

Convert a string to snake case.

Parameters

string $delimiter

Return Value

Stringable

bool startsWith(string|array $needles)

Determine if a given string starts with a given substring.

Parameters

string|array $needles

Return Value

bool

Stringable studly()

Convert a value to studly caps case.

Return Value

Stringable

Stringable substr(int $start, int|null $length = null)

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

Parameters

int $start
int|null $length

Return Value

Stringable

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

Returns the number of substring occurrences.

Parameters

string $needle
int|null $offset
int|null $length

Return Value

int

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 ucfirst()

Make a string's first character uppercase.

Return Value

Stringable

Stringable whenEmpty(callable $callback)

Execute the given callback if the string is empty.

Parameters

callable $callback

Return Value

Stringable

Stringable whenNotEmpty(callable $callback)

Execute the given callback if the string is not empty.

Parameters

callable $callback

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()

Get the number of words a string contains.

Return Value

int

$this dump()

Dump the string.

Return Value

$this

never dd()

Dump the string and end the script.

Return Value

never

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