Замена вставок
Надо заменить {code_0} на $code[0], {code_1} на $code[1], {htm_0} на $htm[0] ...
$text = "Есть текст, в котором присутствуют вставки типа {code_0}, {code_1}, {htm_0} и т.д.";
$code = array ("замена вставки code_0", "замена вставки code_1");
$htm = array ("замена вставки htm_0");
for ($i=0; $i<count($code); $i++)
{
$text = str_replace ("{code_".$i."}", $code[$i], $text);
}
for ($i=0; $i<count($htm); $i++)
{
$text = str_replace ("{htm_".$i."}", $htm[$i], $text);
}
Вариант 1:
function replacer ($matches)
{
global $code, $htm;
return $matches[1] == ("code" ? $code[$matches[2]] : $htm[$matches[2]]);
}
$text = preg_replace_callback ("/\{(code|htm)_(\d+)\}/i", "replacer", $text);
$text = preg_replace ("/\{([a-z]+)_([0-9]+)\}/e", "isset($\\1[\\2]) ? $\\1[\\2] : ''", $text);





