2013-04-06 117 views
1

我剛剛創建了一個腳本來抓取ip,主機名和日期並放入文本文件中。我想創建另一個腳本來將這些信息顯示在一個帶有一些表格的.php文件中,以便於閱讀。在php/html中解析顯示在表格中的txt文件

這是我用來寫入文件的代碼,它完美的工作。我只是不知道如何解析它來做我想做的事情。

$logFile = 'IPLog.txt'; 
$fh = fopen($logFile,'a') or die("can't open file"); 
$ip = $_SERVER['REMOTE_ADDR']; 
$fullhost = gethostbyaddr($ip); 
$stringData = date('m/d/y | h:ia') . " - " . $ip . ":" . $fullhost . "\n"; 
fwrite($fh, $stringData); 
fclose($fh); 

輸出看起來是這樣的..

13年4月6日| 02:53 pm - xxx.xxx.xxx.xxx:xxx.comcast.net

我正在等待腳本讀取文件並將其顯示在表格中,例如。

IP Address  | Hostname   | Date  | Time 
----------------|-------------------|-------------|----------------------------- 
xxx.xxx.xxx.xx | xxx.comcast.net | 04/06/13 | 02:53pm 
-------------------------------------------------------------------------------- 
xxx.xxx.xxx.xx | xxx.comcast.net | 04/06/13 | 02:53pm 
-------------------------------------------------------------------------------- 
xxx.xxx.xxx.xx | xxx.comcast.net | 04/06/13 | 02:53pm 
-------------------------------------------------------------------------------- 
xxx.xxx.xxx.xx | xxx.comcast.net | 04/06/13 | 02:53pm 
-------------------------------------------------------------------------------- 

所以我希望它只是做一個可愛的小表來顯示信息,以便我可以檢查它真正的快,它看起來不錯。

爲什麼我想要這個沒有什麼特別的理由。我只用這個例子來解析一個文本文件。我從來沒有擅長它,真的想知道它是如何完成的。所以我可以用它來做其他事情,如果我也想。

+0

[?你嘗試過什麼] (http://mattgemmell.com/2008/12/08/what-have-you-tried/)向我們展示一些PHP/HTML代碼。檢查[html表格](http://www.quackit.com/html/html_table_tutorial.cfm)結構。另外,你是否想在HTML表中附加'IPLog.txt',或者這只是一個錯誤? – 2013-04-06 20:30:52

+0

你想要的輸出應該是HTML'

'標籤,或者你想要ascii藝術風格?我很困惑。 – complex8572013-04-06 20:33:33

+0

@ complex857我猜對了html表格,直到我看到他實際上將當前信息附加到.txt中...我會等待OP清除它 – 2013-04-06 20:35:22

回答

0

將當前輸出更改爲:04/06/13 - 02:53pm - xxx.xxx.xxx.xxx - www.comcast.net。 這會使後面的解析更容易。 因此,在當前的文件,你必須更改以下行:

$stringData = date('m/d/y - h:ia') . " - " . $ip . " - " . $fullhost . "\n";

我們在表中顯示的數據,可以使用以下命令:

$logFile = 'IPLog.txt'; 
$lines = file($logFile); // Get each line of the file and store it as an array 
$table = '<table border="1"><tr><td>Date</td><td>Time</td><td>IP</td><td>Domain</td></tr>'; // A variable $table, we'll use this to store our table and output it later ! 
foreach($lines as $line){ // We are going to loop through each line 
    list($date, $time, $ip, $domain) = explode(' - ', $line); 
    // What explode basically does is it takes a delimiter, and a string. It will generate an array depending on those two parameters 
    // To explain this I'll provide an example : $array = explode('.', 'a.b.c.d'); 
    // $array will now contain array('a', 'b', 'c', 'd'); 
    // We use list() to give them kind of a "name" 
    // So when we use list($date, $time, $ip, $domain) = explode('.', 'a.b.c.d'); 
    // $date will be 'a', $time will be 'b', $ip will be 'c' and $domain will be 'd' 
    // We could also do it this way: 
    // $data = explode(' - ', $line); 
    // $table .= '<tr><td>'.$data[0].'</td><td>'.$data[1].'</td><td>'.$data[2].'</td><td>'.$data[3].'</td></tr>'; 
    // But the list() technique is much more readable in a way 
    $table .= "<tr><td>$date</td><td>$time</td><td>$ip</td><td>$domain</td></tr>"; 
} 
$table .= '</table>'; 
echo $table; 
+1

哇,我期望更多的編碼方式那麼。這真的很簡單,將研究它真的很好,並瞭解它是如何工作的。謝謝,這正是我想要的,現在我只需要用一些css清理它,並使它看起來不錯。 – 2013-04-06 23:23:31

+0

@JoeBobJr。不客氣,我在代碼中添加了註釋,以解釋我在那裏做了什麼,快樂的編碼! – HamZa 2013-04-06 23:36:15