- preg_replace_callback: documentation ( source)
- str_repeat: documentation ( source)
<?php
$pattern = "/mango|apple|banana/";
$text = "i like banana and apple alot";
echo preg_replace_callback($pattern, function($matches){
$str = $matches[0];
$len = strlen($str);
$stars = str_repeat('*', $len-2);
return $str[0].$stars.$str[$len-1];
}, $text);