2011-05-11 37 views
2

我已經有一個表單上傳文本輸入行到一個.txt文件,並希望顯示文本文件的最後25行與文本輸入窗體(我也想給每行一個隨機左邊距值1-800px之間) - 我該如何實現它?將.txt文件的最後25行發佈到網站上?

+0

你能澄清你的問題嗎? – soandos 2011-05-11 22:46:49

回答

3

我想你會想看看file(),array_slice()rand()

也許是這樣的:

$output = ""; 
$lines = array_slice(file("test.txt"), -25, 25); 

foreach ($lines as $line) 
{ 
    $output .= '<div style="margin-left: ' . rand(1, 800) . 'px;">' . $line . '</div>'; 
} 

echo $output; 
+0

我試圖把它放在一個php包裝中,但它似乎不適用於我,我只是昏暗? – John 2011-05-11 23:26:44

+0

我的不好,我沒有先測試它......更新了代碼並進行了測試,工作正常。 – mikeds 2011-05-12 01:16:49

0

那麼你要麼必須在整個文件作爲線的陣列讀,挑過去的25或使用shell命令tail -n 25。然後在<p>標籤中隨機輸出margin-left樣式。

0
  • 首先調用file讀取文件的內容在一個陣列
  • 然後調用array_slice讀這個數組最後25行。
0

如果你使用的是Unix-y服務器,那麼$lines = array(); $result = exec("tail -25 $filename", $lines)應該會給你你所需要的。輸出可能涉及foreach($lines as $line) { ... }和內聯CSS,如echo '<p style="margin-left:' . rand(1, 800) . 'px;">' . $line . '</p>'.

+0

如果你這樣做,確保$ filename是安全的。 – 2011-05-11 22:54:39