<?php $data = 'this is appLe and ApPle'; $search = 'apple'; $replace = 'pear'; $data = preg_replace_callback('/\b'.$search.'\b/i', function($matches) use ($replace) { $i=0; return join('', array_map(function($char) use ($matches, &$i) { return ctype_lower($matches[0][$i++])?strtolower($char):strtoupper($char); }, str_split($replace))); }, $data); ?>
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more