Yevgeni Polski
Yevgeni Polski

Reputation: 23

Php: Output a delimited text file to HTML table rows

I have a text file with a list of semicolon-delimited items. I'd like for each semicolon to create a new HTML tr. In addition, I would like for commas within the semicolon-delimited items to separate the item into columns (td). The table would have three columns.

How can this be done with php?

Upvotes: 1

Views: 2936

Answers (6)

Theraot
Theraot

Reputation: 40200


Standard approach

Start by splitting your strings... Let's say you have:

$str = 'text, something, value; another, extra, thing; string, content, data';

Note: you can read the file with with file_get_contents.

Now you can use explode on it to get the parts:

$rows = explode(';', $str);
foreach ($rows as $row)
{
    $cells = explode(',', $row);
    //....
}

And output:

$rows = explode(';', $str);
foreach ($rows as $row)
{
    echo '<tr>';
    $cells = explode(',', $row);
    foreach ($cells as $cell)
    {
        echo '<td>'.$cell.'</td>'
    }
    echo '</tr>';
    echo "\n"; // just for presentation
}

That would yield:

<tr><td>text</td><td>something</td><td>value</td></tr>
<tr><td>another</td><td>extra<td><td>thing</td></tr>
<tr><td>string</td><td>content<td><td>data</td></tr>

str_getcsv

From PHP 5.3 and above you can do use str_getcsv:

$rows = str_getcsv($str, ';');
foreach ($rows as $row)
{
    echo '<tr>';
    $cells = str_getcsv(',', $row);
    foreach ($cells as $cell)
    {
        echo '<td>'.$cell.'</td>'
    }
    echo '</tr>';
    echo "\n"; // just for presentation
}

The adventage of using str_getcsv is that it allows you to specify eclosure and escape characters. For example:

$str = '"text", "something", "value"; "another", "look: \"escape sequence\"";

There the enclosure character is " and the escape character is \.

Upvotes: 1

Kai Qing
Kai Qing

Reputation: 18833

you can use file_get_contents, explode, and implode to do this pretty easily:

$data = file_get_contents('file.txt');
$semi = explode(";", $data);
foreach($semi AS $row)
{
    $td = explode(",", $row);
    echo '<tr><td>' . implode('</td><td>', $td) . '</td></tr>';
}

Upvotes: 0

MartyIX
MartyIX

Reputation: 28646

One approach would be:

$input = "1,2,3;4,5,6;7,8,9";

echo "<table>\n<tr><td>".str_replace(array(",", ";"), array("</td><td>", "</td></tr>\n<tr><td>"), $input)."</td></tr></table>";

Result:

<table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
<tr><td>7</td><td>8</td><td>9</td></tr></table>

Upvotes: 2

Eisa Adil
Eisa Adil

Reputation: 1733

Use explode().

$fn = fopen("test.txt","r") or die("fail to open file");

while($row = fgets($fn)) {
  $content = explode( ";", $row);
  echo '<tr>' . $content . '<tr />';
}

fclose( $fn );

Upvotes: 0

Jite
Jite

Reputation: 5847

Use the explode function (http://www.php.net/explode) then loop through the result/s and echo it out in a table.

Upvotes: 0

Louis XIV
Louis XIV

Reputation: 2224

$data = file("your_file.txt");
foreach ($data as $line) {
    $trs = explode(";", $line);
    foreach ($trs as $tr) {
        echo '<tr>';
        $tds = explode(",", $tr);
        foreach ($tds as $td) {
            echo '<td>';
            echo $td;
            echo '</td>';
        }
        echo '</tr>';
    }
}

Upvotes: 0

Related Questions