Create an HTML table from multiple rows of strings

advertisements

I want to take a text file formatted like so:

*note I can change the format of the stored messages, basically there are delimiters and different lines

;68.229.164.10:4/5/2013:Hello
;71.73.174.13:4/6/2013:Oh Hey

(;IPADDRESS:TIMESTAMP:MESSAGE)

and put it in a table that looks like so:

 IP                       Time                  Message
 68.229.164.10            4/6/2013              Hello
 71.73.174.13             4/6/2013              Oh Hey


I prefer to use something like the following:

http://php.net/manual/en/function.fgetcsv.php

And then format the output accordingly.

From Example 1 on the above referenced page:

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>

so, you could do something like this...

<?php
$row = 1;
if (($handle = fopen("path_to_your_data_file", "r")) !== FALSE) {

    echo '<table>';
    echo '<tr><td></td>IP<td>Time</td><td>Message</td></tr>';

    while (($data = fgetcsv($handle, 1000, ":")) !== FALSE) {
        $num = count($data);
        $row++;
        if ($num > 2) {
           echo '<tr>';
           for ($c=0; $c < $num; $c++) {
              echo '<td>'.$data[$c].'</td>';
           }
           echo '</tr>';
        }
    }

    echo '</table>';
    fclose($handle);
}
?>