gpt4 book ai didi

php - 如何用 fwrite() 中的值替换 file_get_contents() 中的变量?

转载 作者:行者123 更新时间:2023-12-05 02:23:23 25 4
gpt4 key购买 nike

我有大表格,用户写入的所有数据都以特殊方式处理。提交后我的表单应该加载模板 php 文件并将表单中的数据添加到其中。所以我的应用程序处理 POST 数据,通过 file_get_contents() 加载 php 模板并通过 fwrite() 将数据写入新的 php 文件。

但是问题来了。 php 模板文件中的变量按原样编写。但是我需要用提交和解析的表单 POST header 中的值替换 php 模板中的变量。

有人知道怎么做吗?

我的简化代码:

-- form.php
<Form Action="process.php" Method="post">
<Input Name="Name1" Type="text" Value="Value1">
<Button Type="submit">Submit</Button>

-- process.php
$Array=array(
"Name1","Name2",//...
);
if(!empty($_POST)){
foreach($Array as $Value){
if(!empty($_POST[$Value])){
$Value=$_POST[$Value];
}}}
...
$Template=file_get_contents("template.php");
$File=fopen("../export/".$userid.".html","w+");
fwrite($File,$Template);
fclose($File);

-- template.php
<!Doctype Html>
...
Name1: <?=$Name1?><Br>
...

我的目标:

-- 135462.html
<!Doctype Html>
...
Name1: Value1
...

最佳答案

我认为您正在寻找 php 缓冲区。ob_* 将帮助您做到这一点。

检查 http://php.net/ob_start

模板.php :

<html>
<head></head>
<body><?=$foo?></body>
</html>

索引.php :

<?php
$foo = $_POST['text'];
ob_start();
include('template.php');
$template_html = ob_get_contents();
ob_end_clean();

//do your stuff

echo $template_html;
?>

关于php - 如何用 fwrite() 中的值替换 file_get_contents() 中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22365457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com