如何使用html / php制作一个简单的日记发布网站

时间:2013-12-06 00:11:57

标签: php html file textarea posting

我正在尝试创建一个简单的日记网站,我将文本输入到文本区域,然后按下提交,它将显示在我当前的屏幕上。然后我希望能够在我的文本区域输入更多文本,当我按下提交时,它只是在新行上显示给我。当我提交3个字符串test,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;
?>

1 个答案:

答案 0 :(得分:0)

尝试添加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);