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
afterafter_lastappendnewLineasciibasenamechar_atclass_basenamebeforebefore_lastbetweencamelcontainscontains_alldirnameends_withexactlyexcerptexplodefinishtrailingSlashuntrailingSlashuntrailingisis_asciiis_jsonis_uuidis_emptyis_not_emptykebablengthlimitlowermarkdowninline_markdownmaskmatchis_matchmatch_alltestpad_bothpad_leftpad_rightpipepluralplural_studlyprependremovereverserepeatreplacereplace_arrayreplace_firstreplace_lastreplace_matchessquishstartstrip_tagsuppertitleheadlinesingularslugsnakestartsWithstudlystudlyUnderscoresubstrsubstr_countswaptrimltrimrtrimlcfirstucfirstwhen_containswhen_contains_allwhen_emptywhen_not_emptywhen_ends_withwhen_exactlywhen_not_exactlywhen_iswhen_is_asciiwhen_is_uuidwhen_starts_withwhen_testwordsword_countwrapdumpddvalueto_integerto_floatto_booleanto_date
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