给定一个像这样的字符串...
$htmlPattern = "User name is: #name# and user company is #company#";
如何将子字符串 #name
# 和 #company
# 替换为变量中的元素?
例如,如果 $name
是 "John"
且 $company
是 Microsoft
,我如何生成字符串 User name is: John 和用户公司是:Microsoft
?
P粉0290579282024-04-07 11:53:08
您可以在彼此内部使用多个 str_replace,如下所示:
User name is: #name# and user company is #company#
P粉3116177632024-04-07 11:21:21
更改数组,使键周围包含 #。然后您可以将其用作
strtr() 的替换参数代码>
.
$myArr = [ ["#name#" => "John", "#company#" => "Microsoft"], ["#name#" => "Erica", "#company#" => "Apple"] ]; foreach ($myArr as $row) { $emptyHtml .= strtr($htmlPattern, $row) }