String functions | piper | Spatie

 SPATIE

  Piper
========

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Piper](https://spatie.be/docs/piper/v1)  Usage  String functions

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/piper/v1)

  String functions
- [ Introduction ](https://spatie.be/docs/piper/v1/introduction)
- [ Installation &amp; setup ](https://spatie.be/docs/piper/v1/installation-setup)
- [ Support us ](https://spatie.be/docs/piper/v1/support-us)
- [ Questions and issues ](https://spatie.be/docs/piper/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/piper/v1/changelog)
- [ About us ](https://spatie.be/docs/piper/v1/about-us)

Usage
-----

- [ Array functions ](https://spatie.be/docs/piper/v1/usage/array-functions)
- [ String functions ](https://spatie.be/docs/piper/v1/usage/string-functions)

 String functions
================

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.

[after](#content-after)[isMatch](#content-ismatch)[remove](#content-remove)[substrReplace](#content-substrreplace)[afterLast](#content-afterlast)[isNotEmpty](#content-isnotempty)[repeat](#content-repeat)[swap](#content-swap)[append](#content-append)[isUlid](#content-isulid)[replace](#content-replace)[take](#content-take)[before](#content-before)[isUuid](#content-isuuid)[replaceArray](#content-replacearray)[test](#content-test)[beforeLast](#content-beforelast)[kebab](#content-kebab)[replaceEnd](#content-replaceend)[title](#content-title)[between](#content-between)[lcfirst](#content-lcfirst)[replaceFirst](#content-replacefirst)[toBase64](#content-tobase64)[betweenFirst](#content-betweenfirst)[length](#content-length)[replaceLast](#content-replacelast)[toBoolean](#content-toboolean)[camel](#content-camel)[limit](#content-limit)[replaceMatches](#content-replacematches)[toFloat](#content-tofloat)[charAt](#content-charat)[lower](#content-lower)[replaceStart](#content-replacestart)[toInteger](#content-tointeger)[contains](#content-contains)[ltrim](#content-ltrim)[reverse](#content-reverse)[toString](#content-tostring)[deduplicate](#content-deduplicate)[mask](#content-mask)[rtrim](#content-rtrim)[trim](#content-trim)[endsWith](#content-endswith)[matchAll](#content-matchall)[scan](#content-scan)[ucfirst](#content-ucfirst)[exactly](#content-exactly)[newLine](#content-newline)[snake](#content-snake)[unwrap](#content-unwrap)[explode](#content-explode)[numbers](#content-numbers)[split](#content-split)[upper](#content-upper)[finish](#content-finish)[padBoth](#content-padboth)[squish](#content-squish)[wordCount](#content-wordcount)[fromBase64](#content-frombase64)[padLeft](#content-padleft)[start](#content-start)[wordWrap](#content-wordwrap)[headline](#content-headline)[padRight](#content-padright)[startsWith](#content-startswith)[words](#content-words)[is](#content-is)[password](#content-password)[stripTags](#content-striptags)[wrap](#content-wrap)[isAscii](#content-isascii)[position](#content-position)[studly](#content-studly)[isEmpty](#content-isempty)[prepend](#content-prepend)[substr](#content-substr)[isJson](#content-isjson)[random](#content-random)[substrCount](#content-substrcount)#### `after`

Returns everything after the given value.

```
'This is my name' |> after('This is'); // ' my name'
```

#### `afterLast`

Returns everything after the last occurrence of the given value.

```
'App\\Models\\User' |> afterLast('\\'); // 'User'
```

#### `append`

Appends the given values to the string.

```
'Laravel' |> append(' Framework'); // 'Laravel Framework'
```

#### `before`

Returns everything before the given value.

```
'This is my name' |> before('my name'); // 'This is '
```

#### `beforeLast`

Returns everything before the last occurrence of the given value.

```
'App\\Models\\User' |> beforeLast('\\'); // 'App\\Models'
```

#### `between`

Returns the portion of the string between two values.

```
'This is my name' |> between('This', 'name'); // ' is my '
```

#### `betweenFirst`

Returns the smallest possible portion of the string between two values.

```
'[first] middle [second]' |> betweenFirst('[', ']'); // 'first'
```

#### `camel`

Converts the string to `camelCase`.

```
'foo_bar' |> camel(); // 'fooBar'
```

#### `charAt`

Returns the character at the given index.

```
'Laravel' |> charAt(1); // 'a'
```

#### `contains`

Determines whether the string contains the given value.

```
'This is my name' |> contains('my'); // true
```

#### `deduplicate`

Replaces consecutive instances of a character with a single instance.

```
'foo--bar---baz' |> deduplicate('-'); // 'foo-bar-baz'
```

#### `endsWith`

Determines whether the string ends with the given value.

```
'Laravel' |> endsWith('vel'); // true
```

#### `exactly`

Determines whether the string exactly matches the given value.

```
'Laravel' |> exactly('Laravel'); // true
```

#### `explode`

Splits the string by the given separator.

```
'a,b,c' |> explode(','); // ['a', 'b', 'c']
```

#### `finish`

Adds a single instance of the given value to the end of the string.

```
'this/string' |> finish('/'); // 'this/string/'
```

#### `fromBase64`

Decodes a Base64 encoded string.

```
'TGFyYXZlbA==' |> fromBase64(); // 'Laravel'
```

#### `headline`

Converts the string to headline case.

```
'email_notification_sent' |> headline(); // 'Email Notification Sent'
```

#### `is`

Determines whether the string matches a pattern.

```
'foo/bar' |> is('foo/*'); // true
```

#### `isAscii`

Determines whether the string is 7 bit ASCII.

```
'Laravel' |> isAscii(); // true
```

#### `isEmpty`

Determines whether the string is empty.

```
'' |> isEmpty(); // true
```

#### `isJson`

Determines whether the string is valid JSON.

```
'{"name":"Taylor"}' |> isJson(); // true
```

#### `isMatch`

Determines whether the string matches a regular expression.

```
'Laravel' |> isMatch('/Lara/'); // true
```

#### `isNotEmpty`

Determines whether the string is not empty.

```
'Laravel' |> isNotEmpty(); // true
```

#### `isUlid`

Determines whether the string is a valid ULID.

```
'01ARZ3NDEKTSV4RRFFQ69G5FAV' |> isUlid(); // true
```

#### `isUuid`

Determines whether the string is a valid UUID.

```
'550e8400-e29b-41d4-a716-446655440000' |> isUuid(); // true
```

#### `kebab`

Converts the string to `kebab-case`.

```
'fooBar' |> kebab(); // 'foo-bar'
```

#### `lcfirst`

Makes the first character of the string lowercase.

```
'Laravel' |> lcfirst(); // 'laravel'
```

#### `length`

Returns the length of the string.

```
'Laravel' |> length(); // 7
```

#### `limit`

Truncates the string to the given length.

```
'The Laravel framework' |> limit(11); // 'The Laravel...'
```

#### `lower`

Converts the string to lowercase.

```
'LARAVEL' |> lower(); // 'laravel'
```

#### `ltrim`

Trims the left side of the string.

```
'  Laravel' |> ltrim(); // 'Laravel'
```

#### `mask`

Masks a portion of the string with a repeated character.

```
'taylor@example.com' |> mask('*', 3); // 'tay***************'
```

#### `matchAll`

Returns all regex matches for the string.

```
'abc123def456' |> matchAll('/\d+/'); // ['123', '456']
```

#### `newLine`

Appends one or more new lines to the string.

```
'Laravel' |> newLine(2); // "Laravel\n\n"
```

#### `numbers`

Removes all non-numeric characters from the string.

```
'Phone: +1 (555) 123-4567' |> numbers(); // '15551234567'
```

#### `padBoth`

Pads both sides of the string to the given length.

```
'Laravel' |> padBoth(11, '-'); // '--Laravel--'
```

#### `padLeft`

Pads the left side of the string to the given length.

```
'Laravel' |> padLeft(10, '-'); // '---Laravel'
```

#### `padRight`

Pads the right side of the string to the given length.

```
'Laravel' |> padRight(10, '-'); // 'Laravel---'
```

#### `password`

Generates a secure random password.

```
password(12); // random 12-character password
```

#### `position`

Returns the position of the first occurrence of a substring.

```
'Laravel' |> position('vel'); // 4
```

#### `prepend`

Prepends the given values to the string.

```
'Framework' |> prepend('Laravel '); // 'Laravel Framework'
```

#### `random`

Generates a random string of the given length.

```
random(16); // random 16-character string
```

#### `remove`

Removes the given value from the string.

```
'Laravel Framework' |> remove(' Framework'); // 'Laravel'
```

#### `repeat`

Repeats the string the given number of times.

```
'ab' |> repeat(3); // 'ababab'
```

#### `replace`

Replaces a given value in the string.

```
'Laravel 13.x' |> replace('13.x', '14.x'); // 'Laravel 14.x'
```

#### `replaceArray`

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

#### `replaceEnd`

Replaces the last occurrence of a value if it appears at the end of the string.

```
'Hello World' |> replaceEnd('World', 'Laravel'); // 'Hello Laravel'
```

#### `replaceFirst`

Replaces the first occurrence of a value in the string.

```
'the quick brown fox' |> replaceFirst('the', 'a'); // 'a quick brown fox'
```

#### `replaceLast`

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

#### `replaceMatches`

Replaces substrings matching a regular expression.

```
'(+1) 501-555-1000' |> replaceMatches('/[^A-Za-z0-9]++/', ''); // '15015551000'
```

#### `replaceStart`

Replaces the first occurrence of a value if it appears at the start of the string.

```
'Hello World' |> replaceStart('Hello', 'Laravel'); // 'Laravel World'
```

#### `reverse`

Reverses the string.

```
'Laravel' |> reverse(); // 'levaraL'
```

#### `rtrim`

Trims the right side of the string.

```
'Laravel  ' |> rtrim(); // 'Laravel'
```

#### `scan`

Parses input from the string according to a format.

```
'filename.jpg' |> scan('%[^.].%s'); // ['filename', 'jpg']
```

#### `snake`

Converts the string to `snake_case`.

```
'fooBar' |> snake(); // 'foo_bar'
```

#### `split`

Splits the string using a regular expression.

```
'a b  c' |> split('/\s+/'); // ['a', 'b', 'c']
```

#### `squish`

Removes extra whitespace from the string.

```
"  laravel\tphp   framework  " |> squish(); // 'laravel php framework'
```

#### `start`

Adds a single instance of the given value to the start of the string.

```
'this/string' |> start('/'); // '/this/string'
```

#### `startsWith`

Determines whether the string starts with the given value.

```
'Laravel' |> startsWith('Lar'); // true
```

#### `stripTags`

Removes HTML and PHP tags from the string.

```
'Laravel' |> stripTags(); // 'Laravel'
```

#### `studly`

Converts the string to `StudlyCase`.

```
'foo bar' |> studly(); // 'FooBar'
```

#### `substr`

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

```
'Laravel' |> substr(1, 3); // 'ara'
```

#### `substrCount`

Returns the number of occurrences of a value in the string.

```
'one two one' |> substrCount('one'); // 2
```

#### `substrReplace`

Replaces text within a portion of the string.

```
'1300' |> substrReplace(':', 2, 0); // '13:00'
```

#### `swap`

Replaces multiple values in the string using a map.

```
'framework' |> swap(['frame' => 'pipe', 'work' => 'line']); // 'pipeline'
```

#### `take`

Returns the first or last characters from the string.

```
'Laravel' |> take(-3); // 'vel'
```

#### `test`

Determines whether the string matches a regular expression.

```
'Laravel' |> test('/Lara/'); // true
```

#### `title`

Converts the string to title case.

```
'laravel framework' |> title(); // 'Laravel Framework'
```

#### `toBase64`

Encodes the string as Base64.

```
'Laravel' |> toBase64(); // 'TGFyYXZlbA=='
```

#### `toBoolean`

Casts the string to a boolean.

```
'true' |> toBoolean(); // true
```

#### `toFloat`

Casts the string to a float.

```
'12.5' |> toFloat(); // 12.5
```

#### `toInteger`

Casts the string to an integer.

```
'42' |> toInteger(); // 42
```

#### `toString`

Returns the string value.

```
'Laravel' |> toString(); // 'Laravel'
```

#### `trim`

Trims both sides of the string.

```
'  Laravel  ' |> trim(); // 'Laravel'
```

#### `ucfirst`

Makes the first character of the string uppercase.

```
'laravel' |> ucfirst(); // 'Laravel'
```

#### `unwrap`

Removes the given strings from the beginning and end of the string.

```
'"Laravel"' |> unwrap('"'); // 'Laravel'
```

#### `upper`

Converts the string to uppercase.

```
'laravel' |> upper(); // 'LARAVEL'
```

#### `wordCount`

Returns the number of words in the string.

```
'hello world' |> wordCount(); // 2
```

#### `words`

Limits the number of words in the string.

```
'The Laravel framework' |> words(2); // 'The Laravel...'
```

#### `wordWrap`

Wraps the string to the given number of characters.

```
'The quick brown fox' |> wordWrap(10); // "The quick\nbrown fox"
```

#### `wrap`

Wraps the string with the given strings.

```
'Laravel' |> wrap('"'); // '"Laravel"'
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
