3 回答
![?](http://img1.sycdn.imooc.com/533e4cf4000151f602000200-100-100.jpg)
TA贡献1863条经验 获得超2个赞
使用strtr它将转换字符串的一部分。
$club = "Barcelona";
echo strtr($data_base[0]['body'], array('{$club}' => $club));
对于多个值(Demo):
$data_base[0]['body'] = 'I am a {$club} fan.'; // Tests
$vars = array(
'{$club}' => 'Barcelona',
'{$tag}' => 'sometext',
'{$anothertag}' => 'someothertext'
);
echo strtr($data_base[0]['body'], $vars);
程序输出:
I am a Barcelona fan.
![?](http://img1.sycdn.imooc.com/533e4c9c0001975102200220-100-100.jpg)
TA贡献1765条经验 获得超5个赞
/**
* A function to fill the template with variables, returns filled template.
*
* @param string $template A template with variables placeholders {$varaible}.
* @param array $variables A key => value store of variable names and values.
*
* @return string
*/
public function replaceVariablesInTemplate($template, array $variables){
return preg_replace_callback('#{(.*?)}#',
function($match) use ($variables){
$match[1]=trim($match[1],'$');
return $variables[$match[1]];
},
' '.$template.' ');
}
- 3 回答
- 0 关注
- 1038 浏览
添加回答
举报