spatie/piper
Pipe-operator-first PHP utility library for array and string manipulation. Piper ports many Laravel Collection and Str helpers to standalone functions that work with primitives, so you can compose readable pipelines for filtering, mapping, joining, and more.
All string functions live in the Spatie\Piper\Str namespace. Import them individually or in groups.
use function Spatie\Piper\Str\{prefix, suffix, upper};
Each function takes its subject as the first argument, so it slots naturally into a pipe. The reference below lists every available function in alphabetical order. Click a name to jump straight to its description.
afterReturns everything after the given value.
'This is my name' |> after('This is'); // ' my name'
afterLastReturns everything after the last occurrence of the given value.
'App\\Models\\User' |> afterLast('\\'); // 'User'
appendAppends the given values to the string.
'Laravel' |> append(' Framework'); // 'Laravel Framework'
beforeReturns everything before the given value.
'This is my name' |> before('my name'); // 'This is '
beforeLastReturns everything before the last occurrence of the given value.
'App\\Models\\User' |> beforeLast('\\'); // 'App\\Models'
betweenReturns the portion of the string between two values.
'This is my name' |> between('This', 'name'); // ' is my '
betweenFirstReturns the smallest possible portion of the string between two values.
'[first] middle [second]' |> betweenFirst('[', ']'); // 'first'
camelConverts the string to camelCase.
'foo_bar' |> camel(); // 'fooBar'
charAtReturns the character at the given index.
'Laravel' |> charAt(1); // 'a'
containsDetermines whether the string contains the given value.
'This is my name' |> contains('my'); // true
deduplicateReplaces consecutive instances of a character with a single instance.
'foo--bar---baz' |> deduplicate('-'); // 'foo-bar-baz'
endsWithDetermines whether the string ends with the given value.
'Laravel' |> endsWith('vel'); // true
exactlyDetermines whether the string exactly matches the given value.
'Laravel' |> exactly('Laravel'); // true
explodeSplits the string by the given separator.
'a,b,c' |> explode(','); // ['a', 'b', 'c']
finishAdds a single instance of the given value to the end of the string.
'this/string' |> finish('/'); // 'this/string/'
fromBase64Decodes a Base64 encoded string.
'TGFyYXZlbA==' |> fromBase64(); // 'Laravel'
headlineConverts the string to headline case.
'email_notification_sent' |> headline(); // 'Email Notification Sent'
isDetermines whether the string matches a pattern.
'foo/bar' |> is('foo/*'); // true
isAsciiDetermines whether the string is 7 bit ASCII.
'Laravel' |> isAscii(); // true
isEmptyDetermines whether the string is empty.
'' |> isEmpty(); // true
isJsonDetermines whether the string is valid JSON.
'{"name":"Taylor"}' |> isJson(); // true
isMatchDetermines whether the string matches a regular expression.
'Laravel' |> isMatch('/Lara/'); // true
isNotEmptyDetermines whether the string is not empty.
'Laravel' |> isNotEmpty(); // true
isUlidDetermines whether the string is a valid ULID.
'01ARZ3NDEKTSV4RRFFQ69G5FAV' |> isUlid(); // true
isUuidDetermines whether the string is a valid UUID.
'550e8400-e29b-41d4-a716-446655440000' |> isUuid(); // true
kebabConverts the string to kebab-case.
'fooBar' |> kebab(); // 'foo-bar'
lcfirstMakes the first character of the string lowercase.
'Laravel' |> lcfirst(); // 'laravel'
lengthReturns the length of the string.
'Laravel' |> length(); // 7
limitTruncates the string to the given length.
'The Laravel framework' |> limit(11); // 'The Laravel...'
lowerConverts the string to lowercase.
'LARAVEL' |> lower(); // 'laravel'
ltrimTrims the left side of the string.
' Laravel' |> ltrim(); // 'Laravel'
maskMasks a portion of the string with a repeated character.
'taylor@example.com' |> mask('*', 3); // 'tay***************'
matchAllReturns all regex matches for the string.
'abc123def456' |> matchAll('/\d+/'); // ['123', '456']
newLineAppends one or more new lines to the string.
'Laravel' |> newLine(2); // "Laravel\n\n"
numbersRemoves all non-numeric characters from the string.
'Phone: +1 (555) 123-4567' |> numbers(); // '15551234567'
padBothPads both sides of the string to the given length.
'Laravel' |> padBoth(11, '-'); // '--Laravel--'
padLeftPads the left side of the string to the given length.
'Laravel' |> padLeft(10, '-'); // '---Laravel'
padRightPads the right side of the string to the given length.
'Laravel' |> padRight(10, '-'); // 'Laravel---'
passwordGenerates a secure random password.
password(12); // random 12-character password
positionReturns the position of the first occurrence of a substring.
'Laravel' |> position('vel'); // 4
prependPrepends the given values to the string.
'Framework' |> prepend('Laravel '); // 'Laravel Framework'
randomGenerates a random string of the given length.
random(16); // random 16-character string
removeRemoves the given value from the string.
'Laravel Framework' |> remove(' Framework'); // 'Laravel'
repeatRepeats the string the given number of times.
'ab' |> repeat(3); // 'ababab'
replaceReplaces a given value in the string.
'Laravel 13.x' |> replace('13.x', '14.x'); // 'Laravel 14.x'
replaceArrayReplaces a value in the string sequentially using an array.
'The event is from ? to ?' |> replaceArray('?', ['8:30', '9:00']); // 'The event is from 8:30 to 9:00'
replaceEndReplaces the last occurrence of a value if it appears at the end of the string.
'Hello World' |> replaceEnd('World', 'Laravel'); // 'Hello Laravel'
replaceFirstReplaces the first occurrence of a value in the string.
'the quick brown fox' |> replaceFirst('the', 'a'); // 'a quick brown fox'
replaceLastReplaces the last occurrence of a value in the string.
'the quick brown fox jumps over the lazy dog' |> replaceLast('the', 'a'); // 'the quick brown fox jumps over a lazy dog'
replaceMatchesReplaces substrings matching a regular expression.
'(+1) 501-555-1000' |> replaceMatches('/[^A-Za-z0-9]++/', ''); // '15015551000'
replaceStartReplaces the first occurrence of a value if it appears at the start of the string.
'Hello World' |> replaceStart('Hello', 'Laravel'); // 'Laravel World'
reverseReverses the string.
'Laravel' |> reverse(); // 'levaraL'
rtrimTrims the right side of the string.
'Laravel ' |> rtrim(); // 'Laravel'
scanParses input from the string according to a format.
'filename.jpg' |> scan('%[^.].%s'); // ['filename', 'jpg']
snakeConverts the string to snake_case.
'fooBar' |> snake(); // 'foo_bar'
splitSplits the string using a regular expression.
'a b c' |> split('/\s+/'); // ['a', 'b', 'c']
squishRemoves extra whitespace from the string.
" laravel\tphp framework " |> squish(); // 'laravel php framework'
startAdds a single instance of the given value to the start of the string.
'this/string' |> start('/'); // '/this/string'
startsWithDetermines whether the string starts with the given value.
'Laravel' |> startsWith('Lar'); // true
stripTagsRemoves HTML and PHP tags from the string.
'<p>Laravel</p>' |> stripTags(); // 'Laravel'
studlyConverts the string to StudlyCase.
'foo bar' |> studly(); // 'FooBar'
substrReturns the portion of the string specified by start and length.
'Laravel' |> substr(1, 3); // 'ara'
substrCountReturns the number of occurrences of a value in the string.
'one two one' |> substrCount('one'); // 2
substrReplaceReplaces text within a portion of the string.
'1300' |> substrReplace(':', 2, 0); // '13:00'
swapReplaces multiple values in the string using a map.
'framework' |> swap(['frame' => 'pipe', 'work' => 'line']); // 'pipeline'
takeReturns the first or last characters from the string.
'Laravel' |> take(-3); // 'vel'
testDetermines whether the string matches a regular expression.
'Laravel' |> test('/Lara/'); // true
titleConverts the string to title case.
'laravel framework' |> title(); // 'Laravel Framework'
toBase64Encodes the string as Base64.
'Laravel' |> toBase64(); // 'TGFyYXZlbA=='
toBooleanCasts the string to a boolean.
'true' |> toBoolean(); // true
toFloatCasts the string to a float.
'12.5' |> toFloat(); // 12.5
toIntegerCasts the string to an integer.
'42' |> toInteger(); // 42
toStringReturns the string value.
'Laravel' |> toString(); // 'Laravel'
trimTrims both sides of the string.
' Laravel ' |> trim(); // 'Laravel'
ucfirstMakes the first character of the string uppercase.
'laravel' |> ucfirst(); // 'Laravel'
unwrapRemoves the given strings from the beginning and end of the string.
'"Laravel"' |> unwrap('"'); // 'Laravel'
upperConverts the string to uppercase.
'laravel' |> upper(); // 'LARAVEL'
wordCountReturns the number of words in the string.
'hello world' |> wordCount(); // 2
wordsLimits the number of words in the string.
'The Laravel framework' |> words(2); // 'The Laravel...'
wordWrapWraps the string to the given number of characters.
'The quick brown fox' |> wordWrap(10); // "The quick\nbrown fox"
wrapWraps the string with the given strings.
'Laravel' |> wrap('"'); // '"Laravel"'
How can I help you explore Laravel packages today?