Shortening code
Nah, looks like it was hosting 开发者_如何学编程fault.
Who can make this code shorter?
private function replaceFunc($subject)
{
foreach($this->func as $t)
{
preg_match_all('/\{'.$t.'\([a-zA-Z,\']+\)\}/i', $subject, $res);
for($j = 0; $j < sizeof($res[0]); $j++)
{
preg_match('/\([a-zA-Z,\']+\)/i', $res[0][$j], $match);
if($match > 0)
{
$prep = explode(", ", substr($match[0], 1, -1));
$args = array();
for($i = 0; $i < sizeof($prep); $i++)
{
$args[] = substr($prep[$i], 1, -1);
}
}
else
{
$args = array();
}
$subject = preg_replace('/\{'.$t.preg_quote($match[0]).'\}/i', call_user_func_array($t, $args), $subject);
}
}
return $subject;
}
Have you tried Smarty? It already does what you need and more.
If your in the market for a templating engine, Twig, a new templating engine used by symfony, is much better than smarty IMHO. If your interested in doing more than just simple HTML + foreach loop (it can do that too), Twig has features such as template inheritance, macros, and low performance overhead.
精彩评论