1

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?

1
  • What part of this is confusing? The PHP features needed are all very basic: explode to split the string, and for to loop over the resulting arrays. Commented Dec 13, 2013 at 23:41

6 Answers 6

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome thanks MartyIX and everyone else that replied. I was able to get it working thanks to your help.
1

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 \.

Comments

0
$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>';
    }
}

Comments

0

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

2 Comments

That should be a comment.
How so? Cause its not spoon feeding? I give all the right tools to actually complete the task. Should any answer that is not a complete solution be a comment?
0

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 );

Comments

0

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>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.