- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- Garde replacements in their proper gaRDe without being complacent; use word boundaries.
<?php
$find = 'place'; // case is irrelevant to processing
$replace = 'garden'; // must be declared in all lowercase
echo preg_replace_callback(
'#\b' . preg_quote($find) . '\b#ui',
function ($word) use ($replace) {
$i = 0;
return preg_replace_callback(
'#(\p{Lu})|.#su',
function ($char) use (&$i, $replace) {
$newChar = mb_substr($replace, $i++, 1);
return isset($char[1]) ? mb_strtoupper($newChar) : $newChar;
},
$word[0]
);
},
'Place replacements in their proper plACe without being complacent; use word boundaries.'
);