PHP,写入文件时换行

发布于 2024-11-10 01:46:56 字数 1387 浏览 7 评论 0原文

我试图保留对我编写的表单的所有回复的运行总数,但我在制作时遇到了麻烦,以便每个回复都需要换行。我的代码在下面。我只是想要它,以便更容易阅读,因为现在发生的情况是所有回复都挤在一起,希望每个回复都占一行。我尝试了一些事情,并让它们在代码中进行注释以及结果是什么。提前致谢。

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

摘星┃星的人 2024-11-17 01:46:56

您能否尝试将换行符 (\n) 附加到 $thetext 变量,如下所示:

$thetext=$_POST['sometext'] . "\n";

记住使用“.”作为连接运算符,并在换行符周围使用双引号。

Can you try appending the newline character (\n) to the $thetext variable like this:

$thetext=$_POST['sometext'] . "\n";

Remember to use '.' as the concatenation operator, and use double-quotes around the newline character.

假扮的天使 2024-11-17 01:46:56
$thetext."\n"

在 php 中使用“.”连接字符串,在 javascript 中使用“+”。

$thetext."\n"

in php you concatenate strings using ".", you use "+" in javascript.

救赎№ 2024-11-17 01:46:56

使用换行符“\n”代替 html 的 br

Use newline "\n" instead of the br's which is for html

醉生梦死 2024-11-17 01:46:56

$text = $text."\n" ?

呃,这里还有一些文字来填写答案

$text = $text."\n" ?

Err here's some more text to fill out the answer

維他命╮ 2024-11-17 01:46:56
 fwrite($myfile, $data); // added + "\n" here and responses turned 0

连接字符串运算符是 (.) 而不是 (+)

你也可以这样简化你的脚本

 echo nl2br(get_file_contents($file));
 fwrite($myfile, $data); // added + "\n" here and responses turned 0

the concat string operator is (.) not (+)

you can also simplify your script thusly

 echo nl2br(get_file_contents($file));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文