3 回答

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.

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 關注
- 1053 瀏覽
添加回答
舉報