我想制作一个(模板)系统,所以我需要替换一个值的标签。该模板存储在一个名为“template.tpl”和文件包含以下内容:PHP preg_replace多次
{title}
{description}
{userlist}
{userid} is the id of {username}
{/userlist}
我有以下的PHP脚本重写标签:
$template = file_get_contents('template.tpl');
$template = preg_replace('/{title}/', 'The big user list', $template);
$template = preg_replace('/{description}/', 'The big storage of all the users', $template);
现在我要展开的脚本所以我可以重写{userlist}。我有以下的数据数组:
$array = array(
1 => "Hendriks",
2 => "Peter"
);
如何创建一个脚本,返回例如以下输出?
The big user list
The big storage of all the users
1 is the id of Hendriks
2 is the id of Peter
我希望我已经尽可能清楚地解释了它。
我可以建议寻找Smarty或Twig吗? – Scuzzy
我想你将需要一个表示数组赋值的数组,并且可以检测为每个索引分配了什么类型的内容,以便它可以很聪明并且应用特定的模式匹配,如果说数据是数组vs字符串。例如,你如何知道 {userlist}需要迭代?假设关闭{/ userlist}意味着这个? – Scuzzy
@Scuzzy我只需要这两个功能,所以我认为这比使用Smarty更快。 – LittleStack