Frage

This ASCII-art table is literally what I need to parse:

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

And I have to convert it into HTML like this

    <thead>
        <tr>
            <td>Username</td>
            <td>IP</td>
            <td>Connected</td>
        </tr>
    </thead>
    <tr>
        <td>Test</td>
        <td>127.0.0.1</td>
        <td>Yes</td>
    </tr>
    <tr>
        <td>Atest</td>
        <td>192.168.2.1</td>
        <td>No</td>
    </tr>
    <tr>
        <td>aaa</td>
        <td>1.2.3.4</td>
        <td>Yes</td>
    </tr>
</table>

Of course the content is only an example. The rows can be 40 like only 3.

Problem Solved

War es hilfreich?

Lösung

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("/|/", $value);
                foreach ($value_array as $table_cell_value) {
                    echo '<td>' . $table_cell_value . '</td>';
                }
                echo '</tr>';
            }
        }
        echo '</table>';
    }
}

?>

Andere Tipps

You will have to parse the text file, line by line and split it into fields. Take a look at the explode php function which should help you out by splitting each line into the different columns. It should look something like $columns = explode( "|", $line);

After you run explode, you receive a php array, which you can loop over using for or foreach and then print out the html.

You will probably want to run trim on each of the parts you get back from the explode, in order to remove all the white spaces, and keep the interesting text only.

If you're unsure how to split the text into lines, you can also use explode for that, using something like $lines = explode( "\n", $string );

If you're unsure how to read a text file, checkout the php function file_get_contents

That's it, you should have all the building blocks here and it's time to piece them together yourself :)

One more tip, if you're worried about the horizontal separator lines, you can skip them by comparing the first character of every line, and skip over those that start with \. Something like $line[0] == '\' should do the trick.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top