I have a list of mobile phone $numbers
and i have to change them, prefixing the number 39
, if the number itself starts with one in $prefixes
array.
I don't now how to back referencing to the found prefix, or (it's the same) how to get the matched prefix). I've tried the following but it's not working:
$numbers = array('3284121532', '3478795687'); // the subject
$prefixes = array('328', '347'); // (will be) the pattern
// Build regex for each element of $prefix array
$pattern = array_map(function($s) { return "/^$s/"; }, $prefixes);
$replace = "39\{$1}";
var_dump(preg_replace($pattern, $replace, $numbers);
Any help would be appreciated, thanks.