[Solved] How do I convert highly formatted textual table data into HTML? [closed]


Something like this should work. Example is a text file called my_data.txt which contains the following which is literally based on your example:

\---------\-------------\-------------\
| Username| IP          | Connected   |
\---------\-------------\-------------\
| test    | 127.0.0.1   | Yes         |
| atest   | 192.168.2.1 | No          |
| aaa     | 1.2.3.4     | Yes         |
\---------\-------------\-------------\

Here is the PHP script that reads the file into an array using file and then parses it into a <table>:

<?php

$data = file('my_data.txt');

if (!empty($data)) {
    $value_array = array();
    // Strip out lines like this `\---------\-------------\-------------\`
    $data = preg_replace("/\\|-/", "", $data);
    $data = trim($data);
    if(!empty($data)) {
        echo '<table border="1">';
        foreach ($data as $key => $value) {
            if (!empty($value)) {
                echo '<tr>';
                $value_array = preg_split("https://stackoverflow.com/"/", $value);
                foreach ($value_array as $table_cell_value) {
                    echo '<td>' . $table_cell_value . '</td>';
                }
                echo '</tr>';
            }
        }
        echo '</table>';
    }
}

?>

6

solved How do I convert highly formatted textual table data into HTML? [closed]