我正在尝试制作一个简单的日记网站,在这里我将文本输入到文本区域,然后推送提交,它将显示在我的当前屏幕上。然后我想能够在我的文本区域输入更多的文本,当我推送提交时,它只会在新行上显示给我。当我提交3个字符串测试、test1和test2时,我得到了以下内容。
Yes the test still works This is a test the test was successful This is a test
我想要这个输出
This is a test
the test was successful
Yes the test still works
这是我的php
<?php
$msg = $_POST["msg"];
$posts = file_get_contents("posts.txt");
chmod("posts.txt", 0777);
$posts = "$msg'r'n" . $posts;
file_put_contents("posts.txt", $posts, FILE_APPEND);
echo $posts;
?>
尝试添加echo nl2br($posts);相反HTML无法识别换行符。
建议从文件中删除最后一行''r''n或执行以下操作以清除底部的流氓行:
// take off the last two characters
$posts = substr($posts, 0, -2));
// convert the newlines
$posts = nl2br($posts);
// output
echo $posts;
修复错误的帖子问题:
// get the message
$msg = $_POST["msg"];
// store the original posts from the file
$original_posts = file_get_contents("posts.txt");
// set permissions (this isn't really required)
chmod("posts.txt", 0777);
// prepend the message to the whole file of posts
$posts = "$msg'r'n" . $original_posts;
// output everything
echo nl2br($posts);
// write the entire file (no prepend) to the text file
file_put_contents("posts.txt", $posts);