Example 1 — Two Patterns, Two Callbacks
Map digit runs and letter runs to different callbacks.
<?php
$out = preg_replace_callback_array([
'/\d+/' => function ($m) {
return '[' . $m[0] . ']';
},
'/[a-z]+/' => function ($m) {
return strtoupper($m[0]);
},
], 'ab12cd');
echo $out, PHP_EOL; How It Works
- First, /\d+/ turns 12 into [12], so the text becomes ab[12]cd.
- Next, /[a-z]+/ uppercases letter runs ab and cd.
- The digits inside [12] are not letters, so they stay.
- The final string is AB[12]CD.

