给定一个像这样的字符串...
$htmlPattern = "User name is: #name# and user company is #company#";
如何将子字符串 #name# 和 #company# 替换为变量中的元素?
例如,如果 $name 是 "John" 且 $company 是 Microsoft,我如何生成字符串 User name is: John 和用户公司是:Microsoft?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您可以在彼此内部使用多个 str_replace,如下所示:
User name is: #name# and user company is #company#"; $res = str_replace("#name#",$name,str_replace("#company#",$company,$htmlPattern)); print($res); ?>更改数组,使键周围包含
#。然后您可以将其用作strtr() 的替换参数代码>.$myArr = [ ["#name#" => "John", "#company#" => "Microsoft"], ["#name#" => "Erica", "#company#" => "Apple"] ]; foreach ($myArr as $row) { $emptyHtml .= strtr($htmlPattern, $row) }