Skip to content

Commit d09a3a1

Browse files
committed
document replace matches
1 parent 3ea52fd commit d09a3a1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

strings.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Laravel includes a variety of functions for manipulating string values. Many of
7676
[Str::replaceArray](#method-str-replace-array)
7777
[Str::replaceFirst](#method-str-replace-first)
7878
[Str::replaceLast](#method-str-replace-last)
79+
[Str::replaceMatches](#method-str-replace-matches)
7980
[Str::replaceStart](#method-str-replace-start)
8081
[Str::replaceEnd](#method-str-replace-end)
8182
[Str::reverse](#method-str-reverse)
@@ -882,6 +883,30 @@ The `Str::replaceLast` method replaces the last occurrence of a given value in a
882883

883884
// the quick brown fox jumps over a lazy dog
884885

886+
<a name="method-str-replace-matches"></a>
887+
#### `Str::replaceMatches()` {.collection-method}
888+
889+
The `Str::replaceMatches` method replaces all portions of a string matching a pattern with the given replacement string:
890+
891+
use Illuminate\Support\Str;
892+
893+
$replaced = Str::replaceMatches(
894+
pattern: '/[^A-Za-z0-9]++/',
895+
replace: '',
896+
subject: '(+1) 501-555-1000'
897+
)
898+
899+
// '15015551000'
900+
901+
The `replaceMatches` method also accepts a closure that will be invoked with each portion of the string matching the given pattern, allowing you to perform the replacement logic within the closure and return the replaced value:
902+
903+
use Illuminate\Support\Str;
904+
905+
$replaced = Str::replaceMatches('/\d/', function (array $matches) {
906+
return '['.$matches[0].']';
907+
}, '123');
908+
909+
// '[1][2][3]'
885910

886911
<a name="method-str-replace-start"></a>
887912
#### `Str::replaceStart()` {.collection-method}

0 commit comments

Comments
 (0)