如何在单独的文本文件中格式化来自 PHP 表单的输出数据?
How to format output data from PHP form in separate text file?
我创建了一个 PHP 表单,它将表单数据条目输出到一个单独的文本文件中。输出的问题是它是纯文本,条目之间没有换行符或空格。我想知道如何格式化输出数据以使其更清晰(可能使用 table 或只是简单的换行符)。
我向我的教授询问了解决方案,他将我转介给 this page. 我确信这就是我正在寻找的,但我无法根据我的需要来形成它。我需要对该页面上的代码进行哪些更改才能使其适用于我?我试过添加和减去代码,但这导致了语法错误。
这是 PHP 表单代码:
<?php
// define variables and set to empty values
$nameErr = '';
$emailErr = '';
$commentErr = '';
$likesErr = '';
$howErr = '';
$rateErr = '';
$name = '';
$email = '';
$comment = '';
$likes = '';
$how = '';
$rate = '';
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
if (empty($_POST["comment"])) {
$commentErr = "Comments are required";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["likes"])) {
$likesErr = "Things you liked is required";
} else {
$likes = test_input($_POST["likes"]);
}
if (empty($_POST["how"])) {
$howErr = "How you got to our site is required";
} else {
$how = test_input($_POST["how"]);
}
if (empty($_POST["rate"])) {
$rateErr = "Rating our site is required";
} else {
$rate = test_input($_POST["rate"]);
}
}
function resetForm($form) {
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox')
.removeAttr('checked').removeAttr('selected');
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data, FILE_APPEND | LOCK_EX);//this appends the new data to the file and locks it while doing so to prevent multiple access to thje file at the same time.
}//return an error message if the data isnt a string
}
$data = $name.$email.$comment.$likes.$how.$rate.
'';
file_write($data, 'feedback.php')
?>
编辑 1:
我认为这是导致问题的部分,因为有两个 file_write
:
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data, FILE_APPEND | LOCK_EX);
}//return an error message if the data isnt a string
}
$data = $name.PHP_EOL.$email.PHP_EOL.$comment.PHP_EOL.$likes.PHP_EOL.$how.PHP_EOL.$rate.PHP_EOL.
file_write($data, 'feedback.php');
为了这个答案,我假设 file_write
是您在某处包含的函数。 AFAIK file_write
不是用于写入文件的标准 PHP 函数。
因为您没有指定任何空格或换行符,所以您的数据以单行形式写入文件,没有空格。
如果您希望在每个字段之间换行,请执行以下操作:
$data = $name.PHP_EOL.$email.PHP_EOL.$comment.PHP_EOL.$likes.PHP_EOL.$how.PHP_EOL.$rate;
file_write($data, 'feedback.php');
加换行最好用PHP_EOL
。它是跨平台的——PHP 将自动为您的代码 运行 在哪个平台上选择正确的换行符。
试试“\n”或者你也可以试试“\r\n”
所以你可以按照这个代码:
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data.'\n\n', FILE_APPEND | LOCK_EX);
}//return an error message if the data isnt a string
}
$data = $name.'\n'.$email.'\n'.$comment.'\n'.$likes.'\n'.$how.'\n'.$rate;
第一条评论。保留 FILE_APPEND 标志,因为它将保持持续存储。
其次。将来,如果您想分析用换行符分隔的数据,解析起来会很痛苦。我建议只为新条目导出以换行符分隔的逗号。然后,您可以轻松地解析或导入(作为 csv)到 Excel.
$data = $name.','.$email.','.$comment.','.$likes.','.$how.','.$rate.PHP_EOL;
file_write($data, 'feedback.php');
但是,您可能 运行 遇到有人在您的 Web 表单中放置逗号的问题。你如何解释这一点?您可以用引号将行中的每个项目括起来。你可以自己做那个!祝你好运。
您可以使用 PHP_EOL
常量,以便输出适合服务器的行结尾,在这种情况下您的代码看起来像
$data = $_POST['name'] . PHP_EOL;
$data .= $_POST['email'] . PHP_EOL;
$data .= $_POST['comment'] . PHP_EOL;
$data .= $_POST['likes'] . PHP_EOL;
$data .= $_POST['how'] . PHP_EOL;
$data .= $_POST['rate'] . PHP_EOL;
或者试试这个
$data = $_POST['name'] . "\r\n";
如果这不能帮助您也试试这个
$data = sprintf("%s\n%s\n%s\n",$_POST['name'],$_POST['email'], $_POST['comment'],$_POST['how'],$_POST['rate']);
file_put_contents($file, $data, FILE_APPEND);
你可以试试这个:
<?php
// define variables and set to empty values
$nameErr = '';
$emailErr = '';
$commentErr = '';
$likesErr = '';
$howErr = '';
$rateErr = '';
$name = '';
$email = '';
$comment = '';
$likes = '';
$how = '';
$rate = '';
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
if (empty($_POST["comment"])) {
$commentErr = "Comments are required";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["likes"])) {
$likesErr = "Things you liked is required";
} else {
$likes = test_input($_POST["likes"]);
}
if (empty($_POST["how"])) {
$howErr = "How you got to our site is required";
} else {
$how = test_input($_POST["how"]);
}
if (empty($_POST["rate"])) {
$rateErr = "Rating our site is required";
} else {
$rate = test_input($_POST["rate"]);
}
}
function resetForm($form) {
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox')
.removeAttr('checked').removeAttr('selected');
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data, FILE_APPEND | LOCK_EX);//this appends the new data to the file and locks it while doing so to prevent multiple access to thje file at the same time.
}//return an error message if the data isnt a string
}
$data = $name.PHP_EOL;
$data .= $email.PHP_EOL;
$data .= $comment.PHP_EOL;
$data .= $likes.PHP_EOL;
$data .= $how.PHP_EOL;
$data .= $rate.PHP_EOL;
file_write($data, 'feedback.php')
?>
Hope any of this help you
我创建了一个 PHP 表单,它将表单数据条目输出到一个单独的文本文件中。输出的问题是它是纯文本,条目之间没有换行符或空格。我想知道如何格式化输出数据以使其更清晰(可能使用 table 或只是简单的换行符)。
我向我的教授询问了解决方案,他将我转介给 this page. 我确信这就是我正在寻找的,但我无法根据我的需要来形成它。我需要对该页面上的代码进行哪些更改才能使其适用于我?我试过添加和减去代码,但这导致了语法错误。
这是 PHP 表单代码:
<?php
// define variables and set to empty values
$nameErr = '';
$emailErr = '';
$commentErr = '';
$likesErr = '';
$howErr = '';
$rateErr = '';
$name = '';
$email = '';
$comment = '';
$likes = '';
$how = '';
$rate = '';
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
if (empty($_POST["comment"])) {
$commentErr = "Comments are required";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["likes"])) {
$likesErr = "Things you liked is required";
} else {
$likes = test_input($_POST["likes"]);
}
if (empty($_POST["how"])) {
$howErr = "How you got to our site is required";
} else {
$how = test_input($_POST["how"]);
}
if (empty($_POST["rate"])) {
$rateErr = "Rating our site is required";
} else {
$rate = test_input($_POST["rate"]);
}
}
function resetForm($form) {
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox')
.removeAttr('checked').removeAttr('selected');
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data, FILE_APPEND | LOCK_EX);//this appends the new data to the file and locks it while doing so to prevent multiple access to thje file at the same time.
}//return an error message if the data isnt a string
}
$data = $name.$email.$comment.$likes.$how.$rate.
'';
file_write($data, 'feedback.php')
?>
编辑 1:
我认为这是导致问题的部分,因为有两个 file_write
:
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data, FILE_APPEND | LOCK_EX);
}//return an error message if the data isnt a string
}
$data = $name.PHP_EOL.$email.PHP_EOL.$comment.PHP_EOL.$likes.PHP_EOL.$how.PHP_EOL.$rate.PHP_EOL.
file_write($data, 'feedback.php');
为了这个答案,我假设 file_write
是您在某处包含的函数。 AFAIK file_write
不是用于写入文件的标准 PHP 函数。
因为您没有指定任何空格或换行符,所以您的数据以单行形式写入文件,没有空格。
如果您希望在每个字段之间换行,请执行以下操作:
$data = $name.PHP_EOL.$email.PHP_EOL.$comment.PHP_EOL.$likes.PHP_EOL.$how.PHP_EOL.$rate;
file_write($data, 'feedback.php');
加换行最好用PHP_EOL
。它是跨平台的——PHP 将自动为您的代码 运行 在哪个平台上选择正确的换行符。
试试“\n”或者你也可以试试“\r\n” 所以你可以按照这个代码:
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data.'\n\n', FILE_APPEND | LOCK_EX);
}//return an error message if the data isnt a string
}
$data = $name.'\n'.$email.'\n'.$comment.'\n'.$likes.'\n'.$how.'\n'.$rate;
第一条评论。保留 FILE_APPEND 标志,因为它将保持持续存储。
其次。将来,如果您想分析用换行符分隔的数据,解析起来会很痛苦。我建议只为新条目导出以换行符分隔的逗号。然后,您可以轻松地解析或导入(作为 csv)到 Excel.
$data = $name.','.$email.','.$comment.','.$likes.','.$how.','.$rate.PHP_EOL;
file_write($data, 'feedback.php');
但是,您可能 运行 遇到有人在您的 Web 表单中放置逗号的问题。你如何解释这一点?您可以用引号将行中的每个项目括起来。你可以自己做那个!祝你好运。
您可以使用 PHP_EOL
常量,以便输出适合服务器的行结尾,在这种情况下您的代码看起来像
$data = $_POST['name'] . PHP_EOL;
$data .= $_POST['email'] . PHP_EOL;
$data .= $_POST['comment'] . PHP_EOL;
$data .= $_POST['likes'] . PHP_EOL;
$data .= $_POST['how'] . PHP_EOL;
$data .= $_POST['rate'] . PHP_EOL;
或者试试这个
$data = $_POST['name'] . "\r\n";
如果这不能帮助您也试试这个
$data = sprintf("%s\n%s\n%s\n",$_POST['name'],$_POST['email'], $_POST['comment'],$_POST['how'],$_POST['rate']);
file_put_contents($file, $data, FILE_APPEND);
你可以试试这个:
<?php
// define variables and set to empty values
$nameErr = '';
$emailErr = '';
$commentErr = '';
$likesErr = '';
$howErr = '';
$rateErr = '';
$name = '';
$email = '';
$comment = '';
$likes = '';
$how = '';
$rate = '';
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
if (empty($_POST["comment"])) {
$commentErr = "Comments are required";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["likes"])) {
$likesErr = "Things you liked is required";
} else {
$likes = test_input($_POST["likes"]);
}
if (empty($_POST["how"])) {
$howErr = "How you got to our site is required";
} else {
$how = test_input($_POST["how"]);
}
if (empty($_POST["rate"])) {
$rateErr = "Rating our site is required";
} else {
$rate = test_input($_POST["rate"]);
}
}
function resetForm($form) {
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox')
.removeAttr('checked').removeAttr('selected');
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data, FILE_APPEND | LOCK_EX);//this appends the new data to the file and locks it while doing so to prevent multiple access to thje file at the same time.
}//return an error message if the data isnt a string
}
$data = $name.PHP_EOL;
$data .= $email.PHP_EOL;
$data .= $comment.PHP_EOL;
$data .= $likes.PHP_EOL;
$data .= $how.PHP_EOL;
$data .= $rate.PHP_EOL;
file_write($data, 'feedback.php')
?>
Hope any of this help you