Solution:
One option would be to use preg_replace
with the regex pattern ([A-Z][a-z])\S+
, and then replace with just the captured first two letters.
$str = 'Mukesh Dubey';
$output = preg_replace("/([A-Z][a-z])\S+/", "$1", $str);
echo $str . "\n" . $output;
This prints: Mu Du
Trivially, to cater to retaining any two starting letters, regardless of case, we can try:
$str = 'wolly dolly';
$output = preg_replace("/([A-Za-z]{2})\S+/", "$1", $str);
echo $str . "\n" . $output;