1 回答

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
只是用來str_replace()替換變量。
$string_from_db = 'Hi, I\'m $username from company. How can I help you ?';
echo str_replace('$username', $username, $string_from_db);
輸出:
Hi, I'm Michael from company. How can I help you ?
如果您有多個(gè)不同的值,您可能想要更改,您可以將數(shù)組提供給str_replace().
$string_from_db = 'Hi, I\'m $username from company. How can I help you ?';
$replacements = [
'from' => [
'$username',
'$other_var'
],
'to' => [
$username,
$other_var
]
];
echo str_replace($replacements['from'], $replacements['to'], $string_from_db);
- 1 回答
- 0 關(guān)注
- 141 瀏覽
添加回答
舉報(bào)