ale*_*kva 3 php forms fwrite file-get-contents
我有大表格,用户写入的所有数据都以特殊方式处理。提交后我的表单应加载模板 php 文件并将表单中的数据添加到其中。所以我的应用程序处理 POST 数据,通过加载 php 模板file_get_contents(),并将fwrite()数据写入新的 php 文件。
但问题来了。php 模板文件中的变量按原样编写。但我需要用提交和解析的表单 POST 标头中的值替换 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>
...
Run Code Online (Sandbox Code Playgroud)
我的目标:
-- 135462.html
<!Doctype Html>
...
Name1: Value1
...
Run Code Online (Sandbox Code Playgroud)
我认为你正在寻找 php 缓冲区。ob_* 将帮助您做到这一点。
模板.php:
<html>
<head></head>
<body><?=$foo?></body>
</html>
Run Code Online (Sandbox Code Playgroud)
索引.php:
<?php
$foo = $_POST['text'];
ob_start();
include('template.php');
$template_html = ob_get_contents();
ob_end_clean();
//do your stuff
echo $template_html;
?>
Run Code Online (Sandbox Code Playgroud)