我有一个变量$email
需要替换<!--email-->
我的脚本中的所有变量。
有人可以指导我最好的方法吗,目前我正在做:
$email = $_SESSION["email"];
str_replace("<!--email-->",$email,"<!--email-->")
$email = $_SESSION["email"];
$filename = 'script.php';
$script = str_replace("<!--email-->",$email,file_get_contents($filename));
它将更改您文件中的所有内容,您只需保存或回显它
这是一个 4 班轮,它基本上会抓住你的所有<!--tag-->
并用适当命名的变量替换它们:
$string = "string or output from file_get_contents()";
preg_match_all("/<!--([A-z0-9_]+)-->/", $string, $matches);
foreach($matches[1] as $match){
$string = preg_replace("/<!--([$match]+)-->/i", ${$match}, $string);
}
echo $string;