3v4l.org

run code in 300+ PHP versions simultaneously
<?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);

preferences:
34.75 ms | 402 KiB | 5 Q