PHP,写入文件时换行
我试图保留对我编写的表单的所有回复的运行总数,但我在制作时遇到了麻烦,以便每个回复都需要换行。我的代码在下面。我只是想要它,以便更容易阅读,因为现在发生的情况是所有回复都挤在一起,希望每个回复都占一行。我尝试了一些事情,并让它们在代码中进行注释以及结果是什么。提前致谢。
<?php
if (isset($_POST['sometext']))
{
$myFile = "testFile.txt";
$thetext=$_POST['sometext'] ;//added + "\n" here but all response turned to 0
writemyfile($myFile,$thetext,"a");
} else
{
$thetext="Enter text here";
}
function readmyfile($thefile)
{
$file = fopen($thefile, "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
}
function writemyfile($thefilename,$data,$mode)
{
$myfile=fopen($thefilename,$mode);
fwrite($myfile, $data); // added + "\n" here and responses turned 0
fclose($myfile);
}
?>
<html>
<head>
<title> Zain's Test Site</title></head>
<body>
<form method="post" action="<?php echo $php_self ?>">
<input type="text" name="sometext" value="<?php echo $thetext ?>" >
<input type="submit" name="Submit" value="Click this button">
</form>
<?php readmyfile("testFile.txt"); ?>
</body>
I am trying to keep a running total of all the responses to a form I have written, but I am having trouble making it so that each response takes a new line. I have my code down below. I just want it so that it is easier to read because right now what happens is that all the responses are jammed together would like to have each one on a new line. I tried a few things and have them commented in the code and what the result was. Thanks in Advance.
<?php
if (isset($_POST['sometext']))
{
$myFile = "testFile.txt";
$thetext=$_POST['sometext'] ;//added + "\n" here but all response turned to 0
writemyfile($myFile,$thetext,"a");
} else
{
$thetext="Enter text here";
}
function readmyfile($thefile)
{
$file = fopen($thefile, "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
}
function writemyfile($thefilename,$data,$mode)
{
$myfile=fopen($thefilename,$mode);
fwrite($myfile, $data); // added + "\n" here and responses turned 0
fclose($myfile);
}
?>
<html>
<head>
<title> Zain's Test Site</title></head>
<body>
<form method="post" action="<?php echo $php_self ?>">
<input type="text" name="sometext" value="<?php echo $thetext ?>" >
<input type="submit" name="Submit" value="Click this button">
</form>
<?php readmyfile("testFile.txt"); ?>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您能否尝试将换行符 (\n) 附加到 $thetext 变量,如下所示:
记住使用“.”作为连接运算符,并在换行符周围使用双引号。
Can you try appending the newline character (\n) to the $thetext variable like this:
Remember to use '.' as the concatenation operator, and use double-quotes around the newline character.
在 php 中使用“.”连接字符串,在 javascript 中使用“+”。
in php you concatenate strings using ".", you use "+" in javascript.
使用换行符“\n”代替 html 的 br
Use newline "\n" instead of the br's which is for html
$text = $text."\n"
?呃,这里还有一些文字来填写答案
$text = $text."\n"
?Err here's some more text to fill out the answer
连接字符串运算符是 (.) 而不是 (+)
你也可以这样简化你的脚本
the concat string operator is (.) not (+)
you can also simplify your script thusly