1

我有一个变量$email需要替换<!--email-->我的脚本中的所有变量。

有人可以指导我最好的方法吗,目前我正在做:

$email = $_SESSION["email"];
str_replace("<!--email-->",$email,"<!--email-->") 
4

2 回答 2

1
$email = $_SESSION["email"];
$filename = 'script.php';
$script = str_replace("<!--email-->",$email,file_get_contents($filename));

它将更改您文件中的所有内容,您只需保存或回显它

于 2012-08-01T01:42:43.167 回答
0

这是一个 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;
于 2012-08-01T02:25:25.297 回答