<?php
$lines = file('bbs1.txt');

if ($_POST['write']) {
$name = htmlspecialchars($_POST['name']);
$contents = htmlspecialchars($_POST['contents']);
$reference = htmlspecialchars($_POST['reference']);
$contents = str_replace("\r\n", "<br>", $contents);
$contents = str_replace("\r", "<br>", $contents);
$contents = str_replace("\n", "<br>", $contents);
$time = date("Y/m/d H:i:s");
$data = "$name\t$contents\t$reference\t$time\n";
array_unshift($lines, $data);
}

foreach($lines as $line) {
$line = rtrim($line);
$items = explode("\t", $line);
print "<p>{$items[0]}{$items[1]}{$items[2]}{$items[3]}</p>\n";
}

$fp = fopen('bbs1.txt', 'w');
foreach($lines as $line) fputs($fp, $line);
fclose($fp);

?>