Skip to main content
Version: 1.x

Stringable

The Stringable class exists in the Mantle\Support namespace and is used to create a stringable object from a string or a stringable object.

For more information see the Laravel docs.

Usage

use function Mantle\Support\Helpers\stringable;

$string = stringable( 'String' ); // String

$string->append( 'append' )->lower(); // stringappend

Available Methods

after

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

use function Mantle\Support\Helpers\stringable;

stringable( 'example here' )->after( 'example ' ); // here

after_last

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

use function Mantle\Support\Helpers\stringable;

stringable( 'example again example here' )->after_last( 'example ' ); // here

append

Append a string or an array of strings to the end of the string.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->append( 'append' ); // exampleappend
stringable( 'example' )->append( [ 'append1', 'append2' ] ); // exampleappend1append2

newLine

Return a string with a new line character.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->newLine(); // example\n
stringable( 'example' )->newLine( 2 ); // example\n\n

ascii

Transliterate a UTF-8 value to ASCII.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->ascii();

basename

Get the trailing name component of the path.

use function Mantle\Support\Helpers\stringable;

stringable( 'Example/Path/To/File.txt' )->basename(); // File.txt

char_at

Get the character at a given index.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->char_at( 0 ); // e

class_basename

Get the class name from a fully qualified class name.

use function Mantle\Support\Helpers\stringable;

stringable( 'Example\Namespace\ClassName' )->class_basename(); // ClassName

before

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

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->before( 'ple' ); // ex

before_last

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

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->before_last( 'ple' ); // exam

between

Return the portion of a string between two given values.

use function Mantle\Support\Helpers\stringable;

stringable( 'abcdefgh' )->between( 'a', 'g' ); // bcdef

camel

Convert a string to camel case.

use function Mantle\Support\Helpers\stringable;

stringable( 'Example Name Here' )->camel(); // exampleNameHere

contains

Check if a string contains a given value.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->contains( 'example' ); // true
stringable( 'example' )->contains( 'Example' ); // true
stringable( 'example' )->contains( 'Example', ignore_case: true ); // true

contains_all

Check if a string contains all of the given values.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->contains_all( [ 'example', 'here' ] ); // false
stringable( 'example here' )->contains_all( [ 'example', 'here' ] ); // true

dirname

Get the directory name of a path.

use function Mantle\Support\Helpers\stringable;

stringable( '/example/path/to/file.txt' )->dirname(); // /example/path/to

ends_with

Check if a string ends with a given value.

use function Mantle\Support\Helpers\stringable;

stringable( 'example here' )->ends_with( 'here' ); // true

exactly

Check if a string is exactly equal to a given value.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->exactly( 'example' ); // true

excerpt

Get a string excerpt from a given string.

use function Mantle\Support\Helpers\stringable;

stringable( 'This is my example' )->excerpt( 'my', [ 'radius' => 3 ] ); // '...is my ex...'

explode

Explode a string into an array.

use function Mantle\Support\Helpers\stringable;

stringable( 'example here|example' )->explode( '|' ); // [ 'example here', 'example' ]

finish

Return the string with a given value appended to the end.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->finish( 'example' ); // exampleexample

trailingSlash

Return the string with a trailing slash.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->trailingSlash(); // example/

untrailingSlash

Return the string without a trailing slash.

use function Mantle\Support\Helpers\stringable;

stringable( 'example/' )->untrailingSlash(); // example

untrailing

Return the string without a given trailing value.

use function Mantle\Support\Helpers\stringable;

stringable( 'example trim' )->untrailing( ' trim' ); // example

is

Check if a string matches a given pattern. Supports * wildcards.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->is( 'example' ); // true
stringable( 'example' )->is( 'ex*' ); // true
stringable( 'example' )->is( 'ex*ample' ); // true
stringable( 'example' )->is( 'ex*ample*' ); // true

is_ascii

Check if a string is ASCII.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->is_ascii(); // true

is_json

Check if a string is JSON.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->is_json(); // false
stringable( '{"example": "example"}' )->is_json(); // true

is_uuid

Check if a string is a valid UUID.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->is_uuid(); // false
stringable( '550e8400-e29b-41d4-a716-446655440000' )->is_uuid(); // true

is_empty

Check if a string is empty.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->is_empty(): bool; // false
stringable( '' )->is_empty(): bool; // true

is_not_empty

Check if a string is not empty.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->is_not_empty(): bool; // true
stringable( '' )->is_not_empty(): bool; // false

kebab

Convert a string to kebab case.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->kebab(); // example
stringable( 'Example Name Here' )->kebab(); // example-name-here

length

Get the length of a string.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->length(): int; // 7

limit

Limit the string to a given number of characters.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->limit( 3 ); // ex...
stringable( 'example' )->limit( 3, '... continue reading?' ); // ex... continue reading?

lower

Convert a string to lowercase.

use function Mantle\Support\Helpers\stringable;

stringable( 'Example' )->lower(); // example

markdown

Convert GitHub flavored Markdown into HTML.

use function Mantle\Support\Helpers\stringable;

stringable( "# Example\nText here" )->markdown(); // <h1>Example</h1><p>Text here</p>

inline_markdown

Convert inline Markdown into HTML.

use function Mantle\Support\Helpers\stringable;

stringable( "# Example\nText here" )->inline_markdown(); // <h1>Example</h1><p>Text here</p>

mask

Masks a portion of a string with a repeated character.

use function Mantle\Support\Helpers\stringable;

stringable( 'example@example.com' )->mask( '*', 3 ); // exa****************

match

Get the string matching the given pattern.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->match( /exam/ ); // exam

is_match

Check if a string matches a given pattern.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->is_match( /exam/ ); // true

match_all

Get the string matching the given pattern.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->match_all( /exam/ ); // [ 'exam' ]

test

Check if a string matches a given pattern.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->test( /exam/ ); // true

pad_both

Pad a string to the left and right to a given length.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->pad_both( '_', 3 ); // ___example___

pad_left

Pad a string to the left to a given length.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->pad_left( '_', 3 ); // ___example

pad_right

Pad a string to the right to a given length.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->pad_right( 3, '_' ); // example___

pipe

Pipe the string to a given callback and return the result as a stringable object.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->pipe( 'md5' ); // md5'd value in Stringable object.

plural

Get the plural form of a string.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->plural(); // examples
stringable( 'example' )->plural( 2 ); // examples
stringable( 'example' )->plural( 1 ); // example

plural_studly

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

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->plural_studly(); // Examples
stringable( 'example' )->plural_studly( 2 ); // Examples
stringable( 'example' )->plural_studly( 1 ); // Example

prepend

Prepend a string or an array of strings to the beginning of the string.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->prepend( 'prepend' ); // prependexample

remove

Remove a string or an array of strings from the string.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->remove( 'exam' ); // ple

reverse

Reverse the string.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->reverse(); // elpmaxe

repeat

Repeat the string a given number of times.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->repeat( 3 ); // exampleexampleexample

replace

Replace all occurrences of a given value in the string with another value.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->replace( 'example', 'replace' ); // replace

replace_array

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

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->replace_array( 'example', [ 'replace1', 'replace2' ] ); // replace1replace2

replace_first

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

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->replace_first( 'example', 'replace' ); // replace

replace_last

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

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->replace_last( 'example', 'replace' ); // replace

replace_matches

Replace all occurrences of a given pattern in the string with a value.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->replace_matches( /([a-z]+)/, 'replace' ); // replace

squish

Remove all whitespace from the beginning and end of a string, and reduce multiple spaces to a single space.

use function Mantle\Support\Helpers\stringable;

stringable( ' example ' )->squish(); // example

start

Return the string with a given value prepended to the beginning.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->start( 'pre' ); // preexample

strip_tags

Strip HTML tags from a string.

use function Mantle\Support\Helpers\stringable;

stringable( '<p>example</p>' )->strip_tags(); // example

upper

Convert a string to uppercase.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->upper(); // EXAMPLE

title

Convert a string to title case.

use function Mantle\Support\Helpers\stringable;

stringable( 'example string here' )->title(); // Example String Here

headline

Convert a string to headline case.

use function Mantle\Support\Helpers\stringable;

stringable( 'example string here' )->headline(); // Example String Here

singular

Get the singular form of a string.

use function Mantle\Support\Helpers\stringable;

stringable( 'examples' )->singular(); // example

slug

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

use function Mantle\Support\Helpers\stringable;

stringable( 'example word here' )->slug(); // example-word-here
stringable( 'example word here' )->slug( '_' ); // example_word_here

snake

Convert a string to snake case.

use function Mantle\Support\Helpers\stringable;

stringable( 'example name here' )->snake(); // example_name_here

startsWith

Check if a string starts with a given value.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->startsWith( 'example' ); // true

studly

Convert a string to studly caps case.

use function Mantle\Support\Helpers\stringable;

stringable( 'example name here' )->studly(); // ExampleNameHere

studlyUnderscore

Convert a string to studly caps case with underscores.

use function Mantle\Support\Helpers\stringable;

stringable( 'example name here' )->studlyUnderscore(); // Example_Name_Here

substr

Get a substring of a string.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->substr( 2, 3 ); // amp

substr_count

Count the number of occurrences of a substring in a string.

use function Mantle\Support\Helpers\stringable;

stringable( 'example here' )->substr_count( 'e' ); // 2

swap

Replace all occurrences of a given value in the string with another value.

use function Mantle\Support\Helpers\stringable;

stringable( 'example words here', [ 'example' => 'some', 'here' => 'there' ] ); // some words there

trim

Trim whitespace from the beginning and end of a string.

use function Mantle\Support\Helpers\stringable;

stringable( ' example ' )->trim(); // example

ltrim

Trim whitespace from the beginning of a string.

use function Mantle\Support\Helpers\stringable;

stringable( ' example ' )->ltrim(); // 'example '

rtrim

Trim whitespace from the end of a string.

use function Mantle\Support\Helpers\stringable;

stringable( ' example ' )->rtrim(); // ' example'

lcfirst

Convert the first character of a string to lowercase.

use function Mantle\Support\Helpers\stringable;

stringable( 'Example' )->lcfirst(); // example

ucfirst

Convert the first character of a string to uppercase.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->ucfirst(); // Example

when_contains

Check if a string contains a given value and execute a callback if it does.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_contains( string|iterable<string> $needles, ?callable $callback, ?callable $default = null );

when_contains_all

Check if a string contains all of the given values and execute a callback if it does.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_contains_all( array<string> $needles, ?callable $callback, ?callable $default = null );

when_empty

Check if a string is empty and execute a callback if it is.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_empty( ?callable $callback, ?callable $default = null );

when_not_empty

Check if a string is not empty and execute a callback if it is.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_not_empty( ?callable $callback, ?callable $default = null );

when_ends_with

Check if a string ends with a given value and execute a callback if it does.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_ends_with( string|iterable<string> $needles, ?callable $callback, ?callable $default = null );

when_exactly

Check if a string is exactly equal to a given value and execute a callback if it is.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_exactly( string $value, ?callable $callback, ?callable $default = null );

when_not_exactly

Check if a string is not exactly equal to a given value and execute a callback if it is.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_not_exactly( string $value, ?callable $callback, ?callable $default = null );

when_is

Check if a string matches a given pattern and execute a callback if it does.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_is( string|iterable<string> $pattern, ?callable $callback, ?callable $default = null );

when_is_ascii

Check if a string is ASCII and execute a callback if it is.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_is_ascii( ?callable $callback, ?callable $default = null );

when_is_uuid

Check if a string is a valid UUID and execute a callback if it is.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_is_uuid( ?callable $callback, ?callable $default = null );

when_starts_with

Check if a string starts with a given value and execute a callback if it does.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_starts_with( string|iterable<string> $needles, ?callable $callback, ?callable $default = null );

when_test

Check if a string matches a given pattern and execute a callback if it does.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->when_test( string $pattern, ?callable $callback, ?callable $default = null );

words

Get the first n words of a string.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->words( int $words = 100, string $end = '...' );

word_count

Get the number of words in a string.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->word_count( string|null $characters = null ): int;

wrap

Wrap a string with a given value.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->wrap( string $before, string|null $after = null );

dump

Dump the string and return it as a stringable object.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->dump();

dd

Dump the string and terminate the script.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->dd(): never;

value

Get the string value.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->value(): string;

to_integer

Convert the string to an integer.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->to_integer(): int;

to_float

Convert the string to a float.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->to_float(): float;

to_boolean

Convert the string to a boolean.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->to_boolean(): bool;

to_date

Convert the string to a Carbon date object.

use function Mantle\Support\Helpers\stringable;

stringable( 'example' )->to_date( string|null $format = null, string|null $tz = null ): ?\Carbon\Carbon;