缩短代码
不,看起来是托管错误。
谁能把这段代码改短一点?
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试过 Smarty 吗?它已经满足了您的需要,甚至更多。
Have you tried Smarty? It already does what you need and more.
如果您在市场上寻找模板引擎,Twig,这是 symfony,比聪明的恕我直言要好得多。如果您感兴趣的不仅仅是简单的 HTML + foreach 循环(它也可以做到),Twig 具有模板继承、宏和低性能开销等功能。
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.