Piper is a pipe operator-first PHP utility library for array and string manipulation. It ports Laravel's collection and string utility methods to standalone functions.
use function Spatie\Piper\Arr\{filter, join, map, values};
use function Spatie\Piper\Str\{prefix, suffix};
[1, 2, 3, 4, 5, 6]
|> filter(fn (int $i) => $i % 2 === 0)
|> map(fn (int $i) => pow($i, 2))
|> values()
|> join(', ', ', and ')
|> prefix('The winning numbers are ')
|> suffix('.');
// "The winning numbers are 4, 16, and 36."Piper can be installed using Composer.
composer require spatie/piperWe invest a lot of resources into creating best in class open source packages.
You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Returns the item after the given value.
[1, 2, 3] |> after(2); // 3Returns the average of the given values.
[['score' => 10], ['score' => 20]] |> average('score'); // 15Alias of average.
[['score' => 10], ['score' => 20]] |> avg('score'); // 15Returns the item before the given value.
[1, 2, 3] |> before(2); // 1Breaks the array into chunks of the given size.
[1, 2, 3, 4, 5] |> chunk(2); // [[1, 2], [3, 4], [5]]Breaks the array into chunks while the callback returns true.
str_split('AABB') |> chunkWhile(fn (string $value, int $key, array $chunk) => $value === end($chunk)); // [['A', 'A'], ['B', 'B']]Collapses an array of arrays into a single array.
[[1, 2], [3, 4]] |> collapse(); // [1, 2, 3, 4]Collapses an array of keyed arrays while preserving keys.
[['name' => 'Taylor'], ['role' => 'admin']] |> collapseWithKeys(); // ['name' => 'Taylor', 'role' => 'admin']Combines the array values as keys with another set of values.
['name', 'role'] |> combine(['Taylor', 'admin']); // ['name' => 'Taylor', 'role' => 'admin']Appends the given values to the end of the array.
[1, 2] |> concat([3, 4]); // [1, 2, 3, 4]Determines whether the array contains the given value or passes the given truth test.
['Taylor', 'Abigail'] |> contains('Taylor'); // trueDetermines whether the array strictly contains the given value.
[1, 2, 3] |> containsStrict('1'); // falseReturns the number of items in the array.
[1, 2, 3] |> count(); // 3Counts values by the result of the given callback.
['laravel', 'php', 'piper'] |> countBy(fn (string $value) => strlen($value)); // [7 => 1, 3 => 1, 5 => 1]Returns the Cartesian product of the array and the given arrays.
[1, 2] |> crossJoin(['a', 'b']); // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]Returns the values that are not present in the given array.
['a', 'b', 'c'] |> diff(['b']); // [0 => 'a', 2 => 'c']Returns key/value pairs that are not present in the given array.
['a' => 1, 'b' => 2] |> diffAssoc(['a' => 1]); // ['b' => 2]Returns key/value pairs that are not present after comparing keys with a callback.
['a' => 1, 'b' => 2] |> diffAssocUsing(['A' => 1], 'strcasecmp'); // ['b' => 2]Returns items whose keys are not present in the given array.
['a' => 1, 'b' => 2] |> diffKeys(['a' => 9]); // ['b' => 2]Returns items whose keys are not present after comparing keys with a callback.
['a' => 1, 'b' => 2] |> diffKeysUsing(['A' => 9], 'strcasecmp'); // ['b' => 2]Returns values that are not present after comparing values with a callback.
['a', 'b'] |> diffUsing(['A'], 'strcasecmp'); // [1 => 'b']Determines whether the array does not contain the given value or pass the given truth test.
['Taylor', 'Abigail'] |> doesntContain('Jess'); // trueDetermines whether the array does not strictly contain the given value.
[1, 2, 3] |> doesntContainStrict('1'); // trueFlattens a multidimensional array using dot notation.
['user' => ['name' => 'Taylor']] |> dot(); // ['user.name' => 'Taylor']Returns duplicate values from the array.
['a', 'b', 'a'] |> duplicates(); // [2 => 'a']Returns duplicate values using strict comparisons.
[1, '1', 1] |> duplicatesStrict(); // [2 => 1]Iterates over each item in the array.
[1, 2, 3] |> each(fn (int $value) => logger($value));Iterates over nested item values by spreading them into the callback.
[[1, 2], [3, 4]] |> eachSpread(fn (int $a, int $b) => logger($a + $b));Verifies that every item in the array is of the expected type.
[1, 2, 3] |> ensure('int'); // [1, 2, 3]Determines whether all items pass the given truth test.
[1, 2, 3] |> every(fn (int $value) => $value > 0); // trueReturns all items except those with the given keys.
['name' => 'Taylor', 'role' => 'admin'] |> except('role'); // ['name' => 'Taylor']Filters the array using the given callback.
[1, 2, 3, 4] |> filter(fn (int $value) => $value > 2); // [2 => 3, 3 => 4]Returns the first item that passes the given truth test.
[1, 2, 3] |> first(fn (int $value) => $value > 1); // 2Returns the first item or throws when no item is found.
[1, 2, 3] |> firstOrFail(fn (int $value) => $value > 2); // 3Returns the first item matching the given key/value constraint.
[['score' => 98], ['score' => 91]] |> firstWhere('score', '>', 95); // ['score' => 98]Maps over the array and flattens the result by one level.
[1, 2] |> flatMap(fn (int $value) => [$value, $value * 10]); // [1, 10, 2, 20]Flattens a multidimensional array.
[1, [2, [3]]] |> flatten(); // [1, 2, 3]Swaps the array keys with their values.
['name' => 'Taylor'] |> flip(); // ['Taylor' => 'name']Returns a copy of the array without the given key.
['name' => 'Taylor', 'role' => 'admin'] |> forget('role'); // ['name' => 'Taylor']Returns the items that would appear on the given page.
[1, 2, 3, 4] |> forPage(2, 2); // [2 => 3, 3 => 4]Decodes a JSON string into an array.
fromJson('{"name":"Taylor"}'); // ['name' => 'Taylor']Returns the value for the given key, or a default value.
['name' => 'Taylor'] |> get('name'); // 'Taylor'Returns an existing value or evaluates and returns a default value.
['name' => 'Taylor'] |> getOrPut('role', 'admin'); // 'admin'Groups the array items by a given key or callback.
[['team' => 'core', 'name' => 'Taylor'], ['team' => 'docs', 'name' => 'Jess']] |> groupBy('team'); // ['core' => [...], 'docs' => [...]]Determines whether the given key exists in the array.
['name' => 'Taylor'] |> has('name'); // trueDetermines whether any of the given keys exist in the array.
['name' => 'Taylor'] |> hasAny('email', 'name'); // trueDetermines whether more than one item passes the given truth test.
[1, 2, 3] |> hasMany(fn (int $value) => $value > 1); // trueDetermines whether exactly one item passes the given truth test.
[1, 2, 3] |> hasSole(fn (int $value) => $value === 2); // trueJoins array values into a string.
[['name' => 'Taylor'], ['name' => 'Abigail']] |> implode('name', ', '); // 'Taylor, Abigail'Returns the values that are present in the given array.
['a', 'b'] |> intersect(['b', 'c']); // [1 => 'b']Returns key/value pairs that are present in the given array.
['a' => 1, 'b' => 2] |> intersectAssoc(['a' => 1]); // ['a' => 1]Returns key/value pairs that are present after comparing keys with a callback.
['a' => 1, 'b' => 2] |> intersectAssocUsing(['A' => 1], 'strcasecmp'); // ['a' => 1]Returns items whose keys are present in the given array.
['a' => 1, 'b' => 2] |> intersectByKeys(['b' => 9]); // ['b' => 2]Returns values that are present after comparing values with a callback.
['a', 'b'] |> intersectUsing(['A'], 'strcasecmp'); // [0 => 'a']Determines whether the array is empty.
[] |> isEmpty(); // trueDetermines whether the array is not empty.
[1] |> isNotEmpty(); // trueJoins array values with a final glue string.
['a', 'b', 'c'] |> join(', ', ' and '); // 'a, b and c'Keys the array by the given key or callback.
[['id' => 1, 'name' => 'Taylor']] |> keyBy('id'); // [1 => ['id' => 1, 'name' => 'Taylor']]Returns all keys from the array.
['name' => 'Taylor', 'role' => 'admin'] |> keys(); // ['name', 'role']Returns the last item that passes the given truth test.
[1, 2, 3] |> last(fn (int $value) => $value < 3); // 2Converts the given value into an array.
make('Taylor'); // ['Taylor']Maps each item using the given callback.
[1, 2, 3] |> map(fn (int $value) => $value * 2); // [2, 4, 6]Maps each item into a new instance of the given class.
$users |> mapInto(UserData::class); // [UserData, UserData, ...]Maps nested item values by spreading them into the callback.
[[1, 2], [3, 4]] |> mapSpread(fn (int $a, int $b) => $a + $b); // [3, 7]Groups values returned by the callback into a dictionary.
$users |> mapToDictionary(fn (array $user) => [$user['team'] => $user['name']]); // ['core' => ['Taylor']]Groups values returned by the callback.
$users |> mapToGroups(fn (array $user) => [$user['team'] => $user['name']]); // ['core' => ['Taylor']]Maps items into key/value pairs.
$users |> mapWithKeys(fn (array $user) => [$user['email'] => $user['name']]); // ['taylor@example.com' => 'Taylor']Returns the maximum value for a given key or callback.
[['score' => 10], ['score' => 20]] |> max('score'); // 20Returns the median value for a given key.
[1, 1, 2, 4] |> median(); // 1.5Merges the given array into the current array.
['a'] |> merge(['b']); // ['a', 'b']Recursively merges the given array into the current array.
['a' => ['x']] |> mergeRecursive(['a' => ['y']]); // ['a' => ['x', 'y']]Returns the minimum value for a given key or callback.
[['score' => 10], ['score' => 20]] |> min('score'); // 10Returns the most frequently occurring value for a given key.
[1, 1, 2, 2, 3] |> mode(); // [1, 2]Creates copies of the array items the given number of times.
['a', 'b'] |> multiply(2); // ['a', 'b', 'a', 'b']Returns every nth item from the array.
[1, 2, 3, 4, 5] |> nth(2); // [1, 3, 5]Returns only the items with the given keys.
['name' => 'Taylor', 'role' => 'admin'] |> only('name'); // ['name' => 'Taylor']Pads the array to the given size with a value.
[1, 2] |> pad(4, 0); // [1, 2, 0, 0]Separates items that pass the truth test from those that do not.
[1, 2, 3, 4] |> partition(fn (int $value) => $value % 2 === 0); // [[1 => 2, 3 => 4], [0 => 1, 2 => 3]]Returns the percentage of items that pass the given truth test.
[1, 2, 3, 4] |> percentage(fn (int $value) => $value > 2); // 50.0Retrieves all values for a given key.
[['name' => 'Taylor'], ['name' => 'Abigail']] |> pluck('name'); // ['Taylor', 'Abigail']Returns and removes the last item from a copy of the array.
[1, 2, 3] |> pop(); // 3Adds an item to the beginning of the array.
['b', 'c'] |> prepend('a'); // ['a', 'b', 'c']Returns the value for a key from a copy of the array.
['name' => 'Taylor'] |> pull('name'); // 'Taylor'Adds one or more items to the end of the array.
[1, 2] |> push(3, 4); // [1, 2, 3, 4]Sets the given key and value on the array.
['name' => 'Taylor'] |> put('role', 'admin'); // ['name' => 'Taylor', 'role' => 'admin']Returns one or more random items from the array.
['a', 'b', 'c'] |> random(); // 'b'Creates an array containing a range of numbers.
range(1, 3); // [1, 2, 3]Reduces the array to a single value.
[1, 2, 3] |> reduce(fn (int $carry, int $value) => $carry + $value, 0); // 6Reduces the array to multiple aggregate values.
[1, 2, 3] |> reduceSpread(fn (int $sum, int $count, int $value) => [$sum + $value, $count + 1], 0, 0); // [6, 3]Reduces the array to a single value while receiving keys.
['a' => 1, 'b' => 2] |> reduceWithKeys(fn (int $carry, int $value, string $key) => $carry + $value, 0); // 3Filters out items using the given callback.
[1, 2, 3, 4] |> reject(fn (int $value) => $value > 2); // [0 => 1, 1 => 2]Replaces items in the array with items from the given array.
['name' => 'Taylor'] |> replace(['name' => 'Abigail']); // ['name' => 'Abigail']Recursively replaces items in the array with items from the given array.
['user' => ['name' => 'Taylor']] |> replaceRecursive(['user' => ['name' => 'Abigail']]); // ['user' => ['name' => 'Abigail']]Reverses the order of the array items.
[1, 2, 3] |> reverse(); // [2 => 3, 1 => 2, 0 => 1]Returns the key of the first item matching the value or callback.
['a' => 1, 'b' => 2] |> search(2); // 'b'Selects the given keys from every item in the array.
$users |> select(['id', 'name']); // [['id' => 1, 'name' => 'Taylor'], ...]Returns and removes the first item from a copy of the array.
[1, 2, 3] |> shift(); // 1Randomly shuffles the array items.
[1, 2, 3] |> shuffle(); // [2, 3, 1]Returns the array without the first given number of items.
[1, 2, 3, 4] |> skip(2); // [2 => 3, 3 => 4]Skips items until the given value or callback is reached.
[1, 2, 3, 4] |> skipUntil(3); // [2 => 3, 3 => 4]Skips items while the given callback returns true.
[1, 2, 3, 4] |> skipWhile(fn (int $value) => $value < 3); // [2 => 3, 3 => 4]Returns a slice of the array starting at the given offset.
[1, 2, 3, 4] |> slice(1, 2); // [1 => 2, 2 => 3]Returns a sliding window view of the array.
[1, 2, 3, 4] |> sliding(3); // [[1, 2, 3], [2, 3, 4]]Returns the only item matching the given truth test.
[1, 2, 3] |> sole(fn (int $value) => $value === 2); // 2Alias of contains for truth tests.
[1, 2, 3] |> some(fn (int $value) => $value === 2); // trueSorts the array values.
[3, 1, 2] |> sort() |> values(); // [1, 2, 3]Sorts the array by a given key or callback.
$users |> sortBy('score') |> values(); // lowest score firstSorts the array descending by a given key or callback.
$users |> sortByDesc('score') |> values(); // highest score firstSorts the array values in descending order.
[3, 1, 2] |> sortDesc() |> values(); // [3, 2, 1]Sorts the array by its keys.
['b' => 2, 'a' => 1] |> sortKeys(); // ['a' => 1, 'b' => 2]Sorts the array by its keys in descending order.
['a' => 1, 'b' => 2] |> sortKeysDesc(); // ['b' => 2, 'a' => 1]Sorts the array keys using a callback.
['b' => 2, 'a' => 1] |> sortKeysUsing('strnatcmp'); // ['a' => 1, 'b' => 2]Returns a slice removed from the array.
[1, 2, 3, 4] |> splice(1, 2); // [2, 3]Splits the array into the given number of groups.
[1, 2, 3, 4, 5] |> split(2); // [[1, 2, 3], [4, 5]]Splits the array into the given number of groups, filling groups as evenly as possible.
[1, 2, 3, 4, 5] |> splitIn(2); // [[1, 2, 3], [4, 5]]Returns the sum of the given values.
[['score' => 10], ['score' => 20]] |> sum('score'); // 30Returns the first or last number of items.
[1, 2, 3, 4] |> take(2); // [1, 2]Returns items until the given value or callback is reached.
[1, 2, 3, 4] |> takeUntil(3); // [1, 2]Returns items while the given callback returns true.
[1, 2, 3, 4] |> takeWhile(fn (int $value) => $value < 3); // [1, 2]Passes the array to a callback and returns the array.
[1, 2, 3] |> tap(fn (array $items) => logger($items)); // [1, 2, 3]Creates an array by invoking a callback a given number of times.
times(3, fn (int $number) => $number * 2); // [2, 4, 6]Converts the value to a plain array.
[1, 2, 3] |> toArray(); // [1, 2, 3]Converts the array to JSON.
['name' => 'Taylor'] |> toJson(); // '{"name":"Taylor"}'Converts the array to pretty-printed JSON.
['name' => 'Taylor'] |> toPrettyJson(); // "{\n \"name\": \"Taylor\"\n}"Maps each item and returns the transformed array.
[1, 2] |> transform(fn (int $value) => $value * 10); // [10, 20]Expands a dot-notated array into a multidimensional array.
['user.name' => 'Taylor'] |> undot(); // ['user' => ['name' => 'Taylor']]Adds the given array to the current array without replacing existing keys.
['a' => 1] |> union(['a' => 2, 'b' => 3]); // ['a' => 1, 'b' => 3]Returns the unique items in the array.
['a', 'b', 'a'] |> unique(); // [0 => 'a', 1 => 'b']Returns the unique items using strict comparisons.
[1, '1', 1] |> uniqueStrict(); // [0 => 1, 1 => '1']Runs the callback unless the given value is truthy.
[1] |> unless(false, fn (array $items) => [...$items, 2]); // [1, 2]Runs the callback unless the array is empty.
[1] |> unlessEmpty(fn (array $items) => [...$items, 2]); // [1, 2]Runs the callback unless the array is not empty.
[] |> unlessNotEmpty(fn (array $items) => ['empty']); // ['empty']Returns the given value without wrapping it in an array.
unwrap(['Taylor']); // ['Taylor']Returns the first item value for a given key.
[['name' => 'Taylor'], ['name' => 'Abigail']] |> value('name'); // 'Taylor'Returns the array with consecutive integer keys.
['a' => 'Taylor', 'b' => 'Abigail'] |> values(); // ['Taylor', 'Abigail']Runs the callback when the given value is truthy.
[1] |> when(true, fn (array $items) => [...$items, 2]); // [1, 2]Runs the callback when the array is empty.
[] |> whenEmpty(fn (array $items) => ['empty']); // ['empty']Runs the callback when the array is not empty.
[1] |> whenNotEmpty(fn (array $items) => [...$items, 2]); // [1, 2]Filters items by a key/value pair.
$users |> where('team', 'core'); // users on the core teamFilters items where a key is between two values.
$users |> whereBetween('score', [90, 100]); // users with scores from 90 to 100Filters items where a key is present in the given values.
$users |> whereIn('name', ['Taylor', 'Jess']); // matching usersFilters items by class or type.
$items |> whereInstanceOf(DateTimeImmutable::class); // DateTimeImmutable instancesFilters items where a key is strictly present in the given values.
$users |> whereInStrict('id', [1, 2]); // users with matching integer IDsFilters items where a key is outside two values.
$users |> whereNotBetween('score', [90, 100]); // users outside the score rangeFilters items where a key is not present in the given values.
$users |> whereNotIn('name', ['Taylor']); // users not named TaylorFilters items where a key is strictly not present in the given values.
$users |> whereNotInStrict('id', [1, 2]); // users without those integer IDsFilters items where a key is not null.
$users |> whereNotNull('email'); // users with an email addressFilters items where a key is null.
$users |> whereNull('email'); // users without an email addressFilters items by a strict key/value comparison.
$users |> whereStrict('active', true); // active usersWraps the given value in an array.
wrap('Taylor'); // ['Taylor']Merges together the values of the array with the values of the given arrays.
[1, 2] |> zip(['a', 'b']); // [[1, 'a'], [2, 'b']]Returns everything after the given value.
'This is my name' |> after('This is'); // ' my name'Returns everything after the last occurrence of the given value.
'App\\Models\\User' |> afterLast('\\'); // 'User'Appends the given values to the string.
'Laravel' |> append(' Framework'); // 'Laravel Framework'Returns everything before the given value.
'This is my name' |> before('my name'); // 'This is 'Returns everything before the last occurrence of the given value.
'App\\Models\\User' |> beforeLast('\\'); // 'App\\Models'Returns the portion of the string between two values.
'This is my name' |> between('This', 'name'); // ' is my 'Returns the smallest possible portion of the string between two values.
'[first] middle [second]' |> betweenFirst('[', ']'); // 'first'Converts the string to camelCase.
'foo_bar' |> camel(); // 'fooBar'Returns the character at the given index.
'Laravel' |> charAt(1); // 'a'Determines whether the string contains the given value.
'This is my name' |> contains('my'); // trueReplaces consecutive instances of a character with a single instance.
'foo--bar---baz' |> deduplicate('-'); // 'foo-bar-baz'Determines whether the string ends with the given value.
'Laravel' |> endsWith('vel'); // trueDetermines whether the string exactly matches the given value.
'Laravel' |> exactly('Laravel'); // trueSplits the string by the given separator.
'a,b,c' |> explode(','); // ['a', 'b', 'c']Adds a single instance of the given value to the end of the string.
'this/string' |> finish('/'); // 'this/string/'Decodes a Base64 encoded string.
'TGFyYXZlbA==' |> fromBase64(); // 'Laravel'Converts the string to headline case.
'email_notification_sent' |> headline(); // 'Email Notification Sent'Determines whether the string matches a pattern.
'foo/bar' |> is('foo/*'); // trueDetermines whether the string is 7 bit ASCII.
'Laravel' |> isAscii(); // trueDetermines whether the string is empty.
'' |> isEmpty(); // trueDetermines whether the string is valid JSON.
'{"name":"Taylor"}' |> isJson(); // trueDetermines whether the string matches a regular expression.
'Laravel' |> isMatch('/Lara/'); // trueDetermines whether the string is not empty.
'Laravel' |> isNotEmpty(); // trueDetermines whether the string is a valid ULID.
'01ARZ3NDEKTSV4RRFFQ69G5FAV' |> isUlid(); // trueDetermines whether the string is a valid UUID.
'550e8400-e29b-41d4-a716-446655440000' |> isUuid(); // trueConverts the string to kebab-case.
'fooBar' |> kebab(); // 'foo-bar'Makes the first character of the string lowercase.
'Laravel' |> lcfirst(); // 'laravel'Returns the length of the string.
'Laravel' |> length(); // 7Truncates the string to the given length.
'The Laravel framework' |> limit(11); // 'The Laravel...'Converts the string to lowercase.
'LARAVEL' |> lower(); // 'laravel'Trims the left side of the string.
' Laravel' |> ltrim(); // 'Laravel'Masks a portion of the string with a repeated character.
'taylor@example.com' |> mask('*', 3); // 'tay***************'Returns all regex matches for the string.
'abc123def456' |> matchAll('/\d+/'); // ['123', '456']Appends one or more new lines to the string.
'Laravel' |> newLine(2); // "Laravel\n\n"Removes all non-numeric characters from the string.
'Phone: +1 (555) 123-4567' |> numbers(); // '15551234567'Pads both sides of the string to the given length.
'Laravel' |> padBoth(11, '-'); // '--Laravel--'Pads the left side of the string to the given length.
'Laravel' |> padLeft(10, '-'); // '---Laravel'Pads the right side of the string to the given length.
'Laravel' |> padRight(10, '-'); // 'Laravel---'Generates a secure random password.
password(12); // random 12-character passwordReturns the position of the first occurrence of a substring.
'Laravel' |> position('vel'); // 4Prepends the given values to the string.
'Framework' |> prepend('Laravel '); // 'Laravel Framework'Generates a random string of the given length.
random(16); // random 16-character stringRemoves the given value from the string.
'Laravel Framework' |> remove(' Framework'); // 'Laravel'Repeats the string the given number of times.
'ab' |> repeat(3); // 'ababab'Replaces a given value in the string.
'Laravel 13.x' |> replace('13.x', '14.x'); // 'Laravel 14.x'Replaces 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'Replaces the last occurrence of a value if it appears at the end of the string.
'Hello World' |> replaceEnd('World', 'Laravel'); // 'Hello Laravel'Replaces the first occurrence of a value in the string.
'the quick brown fox' |> replaceFirst('the', 'a'); // 'a quick brown fox'Replaces 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'Replaces substrings matching a regular expression.
'(+1) 501-555-1000' |> replaceMatches('/[^A-Za-z0-9]++/', ''); // '15015551000'Replaces the first occurrence of a value if it appears at the start of the string.
'Hello World' |> replaceStart('Hello', 'Laravel'); // 'Laravel World'Reverses the string.
'Laravel' |> reverse(); // 'levaraL'Trims the right side of the string.
'Laravel ' |> rtrim(); // 'Laravel'Parses input from the string according to a format.
'filename.jpg' |> scan('%[^.].%s'); // ['filename', 'jpg']Converts the string to snake_case.
'fooBar' |> snake(); // 'foo_bar'Splits the string using a regular expression.
'a b c' |> split('/\s+/'); // ['a', 'b', 'c']Removes extra whitespace from the string.
" laravel\tphp framework " |> squish(); // 'laravel php framework'Adds a single instance of the given value to the start of the string.
'this/string' |> start('/'); // '/this/string'Determines whether the string starts with the given value.
'Laravel' |> startsWith('Lar'); // trueRemoves HTML and PHP tags from the string.
'<p>Laravel</p>' |> stripTags(); // 'Laravel'Converts the string to StudlyCase.
'foo bar' |> studly(); // 'FooBar'Returns the portion of the string specified by start and length.
'Laravel' |> substr(1, 3); // 'ara'Returns the number of occurrences of a value in the string.
'one two one' |> substrCount('one'); // 2Replaces text within a portion of the string.
'1300' |> substrReplace(':', 2, 0); // '13:00'Replaces multiple values in the string using a map.
'framework' |> swap(['frame' => 'pipe', 'work' => 'line']); // 'pipeline'Returns the first or last characters from the string.
'Laravel' |> take(-3); // 'vel'Determines whether the string matches a regular expression.
'Laravel' |> test('/Lara/'); // trueConverts the string to title case.
'laravel framework' |> title(); // 'Laravel Framework'Encodes the string as Base64.
'Laravel' |> toBase64(); // 'TGFyYXZlbA=='Casts the string to a boolean.
'true' |> toBoolean(); // trueCasts the string to a float.
'12.5' |> toFloat(); // 12.5Casts the string to an integer.
'42' |> toInteger(); // 42Returns the string value.
'Laravel' |> toString(); // 'Laravel'Trims both sides of the string.
' Laravel ' |> trim(); // 'Laravel'Makes the first character of the string uppercase.
'laravel' |> ucfirst(); // 'Laravel'Removes the given strings from the beginning and end of the string.
'"Laravel"' |> unwrap('"'); // 'Laravel'Converts the string to uppercase.
'laravel' |> upper(); // 'LARAVEL'Returns the number of words in the string.
'hello world' |> wordCount(); // 2Limits the number of words in the string.
'The Laravel framework' |> words(2); // 'The Laravel...'Wraps the string to the given number of characters.
'The quick brown fox' |> wordWrap(10); // "The quick\nbrown fox"Wraps the string with the given strings.
'Laravel' |> wrap('"'); // '"Laravel"'You can run the tests with:
composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
Piper is a port of Laravel's collection & string helpers. All methods, tests, and documentation examples are derived from the Laravel source. The porting strategy is described in PORT.md.
The MIT License (MIT). Please see License File for more information.
