我有以下格式的文本文件的不同的線路,讓我們stats.txt稱之爲:PHP代碼編輯一個文本文件
valueA1, valueA2, valueA3, valueA4
valueB1, valueB2, valueB3, valueB4
valueC1, valueC2, valueC3, valueC4
valueD1, valueD2, valueD3, valueD4
我想四個文本框或文本域與此代碼中填入一個html文檔和表單提交時,附加到文本文件的相應行。我不是很瞭解PHP,所以無法找到一種方法來做到這一點。
我當前的HTML文件看起來像這樣:
$url = 'editor.php';
$file = 'stats.txt';
// check if form has been submitted
if (isset($_POST['text1']))
{
// save the text contents
file_put_contents($file, $_POST['text1']);
// redirect to form again
header(sprintf('Location: %s', $url));
printf('<a href="%s">Updated</a>.', htmlspecialchars($url));
exit();
}
// read the textfile
$text = file_get_contents($file);
?>
我無法弄清楚文本文件的字裏行間如何循環,故有可怕的代碼這個塊來獲得每條線的值:
<?php
//for Line 1
$myLine1 = 1 ;
$linefile1 = new SplFileObject($file);
$linefile1->seek($myLine1-1);
//for Line 2
$myLine2 = 2 ;
$linefile2 = new SplFileObject($file);
$linefile2->seek($myLine2-1);
//for Line 3
$myLine3 = 3 ;
$linefile3 = new SplFileObject($file);
$linefile3->seek($myLine3-1);
//for Line 4
$myLine4 = 4 ;
$linefile4 = new SplFileObject($file);
$linefile4->seek($myLine4-1);
?>
而且形式如下:
<form action="" method="post">
#stuff 1
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile1) ?></textarea>
#stuff 2
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile2) ?></textarea>
#stuff 3
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile3) ?></textarea>
#stuff 4
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile4) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>
有人可以幫我個是嗎?
你應該使用爆炸()把每個值到一個數組,因爲用逗號分隔。如果您可以控制它的存儲方式,最好使用serailize,這樣它就可以以數組形式存儲了。 – 2014-11-06 01:49:44