The main problem you have is that you are not returning str_replace to a variable. Also str_replace is case sensitive so using string replace to essentially try to replace WELCOME with $lang['welcome'] won't work.
Lastly, preg_match stops at the first match of pattern so if your line consisted of "{L_WELCOME} to my site do you want to {L_LOGIN}" then only the {L_WELCOME} would be replaced.
I haven't tested it, but you could try the code below.
Code: Select all
function template($code)
{
global $lang;
$code_lines = explode("\n", $code);
$n_code_lines = count($code_lines);
for ($i = 0;$i < $n_code_lines;$i++)
{
if (preg_match_all('/\{L_(.*?)\}/i', $code_lines[$i], $m, PREG_SET_ORDER))
{
$n_matches = count($m);
for ($j = 0; $j < $n_matches; $j++)
{
$code_lines[$i] = str_replace($m[$j][0], $lang[strtolower($m[$j][1])], $code_lines[$i]);
}
}
}
$code = implode("\n", $code_lines);
return $code;
}