Given a string like this...
$htmlPattern = "User name is: #name# and user company is #company#";
How to replace the substrings#name# and#company# with elements in a variable?
For example, if$nameis"John"and$companyisMicrosoft, how do I generate the stringUser name is: John and user company is: Microsoft?
You can use multiple str_replaces inside each other like this:
User name is: #name# and user company is #company#"; $res = str_replace("#name#",$name,str_replace("#company#",$company,$htmlPattern)); print($res); ?>Change the array so that it contains
# around the keys. You can then use it asstrtr() 的替换参数代码>.$myArr = [ ["#name#" => "John", "#company#" => "Microsoft"], ["#name#" => "Erica", "#company#" => "Apple"] ]; foreach ($myArr as $row) { $emptyHtml .= strtr($htmlPattern, $row) }