[eluser]arume[/eluser]
[quote author="wiredesignz" date="1208793325"]Yep, Seems Ok. But why not use foreach instead of count() & for?[/quote]
OK. As you wish.
Code:
if (preg_match_all('/\{(\w*)\}/siU', $template, $matches))
{
foreach ($matches[0] as $key => $match)
{
if (($line = $this->lang->line("$matches[1][$key]")) === FALSE) $line = $matches[1][$key];
$template = str_replace($match, $line, $template);
}
}
[quote author="wiredesignz" date="1208793325"]Also are you sure preg_match_all, doesn't just use a repeated preg_match?[/quote]
Pretty sure. I tested it. For a small pack of vars (around 5) preg_match is faster, but preg_match_all wins at higher amounts of vars.
[quote author="wiredesignz" date="1208793325"]Maybe preg_replace_callback would be faster again?[/quote]
Maybe. But then you need to write a function only to pass as parameter to preg_replace_callback or use create_function that makes the code hard to read.
Well, I am not a "performance Zealot", but regular expressions are very slow and if calling functions inside loops is bad, calling regular expressions inside loops must be very bad.