<?php
$bands = array(
1 => 'Metallica',
2 => 'Megadeth',
3 => 'Anthrax',
4 => 'Slayer',
5 => 'Black Sabbath',
);
$text = "My first favorite band is: #band{2}, and after that it's: #band1. My over-all first metal band was: #band{5}, and I sometimes enjoy headbaning while listening to: #band3 or #band{4}.";
$out = preg_replace_callback('!\#band\{(\d+)\}!', 'replace_band', $text);
function replace_band($m){
return $GLOBALS['bands'][$m[1]];
}
print_r($out);