在php/html中解析显示在表格中的txt文件
我刚刚创建了一个脚本来抓取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
--------------------------------------------------------------------------------
所以我希望它只是做一个可爱的小表来显示信息,以便我可以检查它真正的快,它看起来不错。
为什么我想要这个没有什么特别的理由。我只用这个例子来解析一个文本文件。我从来没有擅长它,真的想知道它是如何完成的。所以我可以用它来做其他事情,如果我也想。
将当前输出更改为: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;
哇,我期望更多的编码方式那么。这真的很简单,将研究它真的很好,并了解它是如何工作的。谢谢,这正是我想要的,现在我只需要用一些css清理它,并使它看起来不错。 – 2013-04-06 23:23:31
@JoeBobJr。不客气,我在代码中添加了注释,以解释我在那里做了什么,快乐的编码! – HamZa 2013-04-06 23:36:15
将当前输出更改为: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;
哇,我期望更多的编码方式那么。这真的很简单,将研究它真的很好,并了解它是如何工作的。谢谢,这正是我想要的,现在我只需要用一些css清理它,并使它看起来不错。 – 2013-04-06 23:23:31
@JoeBobJr。不客气,我在代码中添加了注释,以解释我在那里做了什么,快乐的编码! – HamZa 2013-04-06 23:36:15
[?你尝试过什么] (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
你想要的输出应该是HTML'
@ complex857我猜对了html表格,直到我看到他实际上将当前信息附加到.txt中...我会等待OP清除它 – 2013-04-06 20:35:22
回答
将当前输出更改为:
04/06/13 - 02:53pm - xxx.xxx.xxx.xxx - www.comcast.net
。 这会使后面的解析更容易。 因此,在当前的文件,你必须更改以下行:$stringData = date('m/d/y - h:ia') . " - " . $ip . " - " . $fullhost . "\n";
我们在表中显示的数据,可以使用以下命令:
哇,我期望更多的编码方式那么。这真的很简单,将研究它真的很好,并了解它是如何工作的。谢谢,这正是我想要的,现在我只需要用一些css清理它,并使它看起来不错。 – 2013-04-06 23:23:31
@JoeBobJr。不客气,我在代码中添加了注释,以解释我在那里做了什么,快乐的编码! – HamZa 2013-04-06 23:36:15
相关问题