$str = "/hello/{:num}/{:str}/";
$replace = [
"num" => "[0-9]",
"str" => "[a-zA-Z]"
];
$str = preg_replace_callback(
'/{:(.*)}/U',
function ($matches) use ($replace) {
if(isset($replace[$matches[1]])) {
return $replace[$matches[1]];
}
else {
return $matches[0];
}
},
$str
);
echo $str;
ideone.com/6OT3Wl