The use
will do the job..
function my_func($text, $prefix){
$regex = '/xxxx/';
return preg_replace_callback($regex, function($matches) use(&$prefix){
//^^^^^^^^^^^^^
}, $text);
}