像这样的东西:
$vars = array("key" => "value", "key2" => "value2" //..etc);
function ($template, $vars) {
$file = file_get_contents($template);
foreach ($vars as $key => $value) {
str_replace($template //this is where I get confused);
}
}
这个想法是获取模板文件的内容(仅包括 html),然后 foreach 将运行并将作为 vars 数组内“键”的文本替换为 vars 数组内的“值”字段的文本。因此,假设我的模板文件文本中有某个地方,例如“{content}”。该函数应该找到该字符串(包括我知道我没有在示例中指定它们的大括号)并将其替换为数组中的相应值。
我觉得我对 str_replace() 函数的理解不够。PHP.net 也没有多大帮助,据我了解,它是这样的:
str_replace($replacethese, $withthese, $inthisfile);
很简单,但是当我的数组是二维的时,我该怎么做呢?我的“$replacethese”参数必须是 $vars 数组的“键”值。