0

I am using file_get_contents() to import a text file. In the text file, the format goes as below (example):

3434,83,8732722
834,93,4983293
9438,43933,34983

and so forth... basically it follows the pattern: integer, comma to split it, second integer, another comma to split it, third integer, then a new line begins. I need to get this into a table with the format following accordingly. So in other words, I would have a 3 column table and each new line in the text file would be a new row in the table.

This must be transcoded into a simple html table with <table> <tr> and <td>

I have never worked with multidimensional arrays and splitting text with that. This is why I'm seeking assistance. I really appreciate it! :)

Bren
  • 605
  • 4
  • 15
  • All I was able to do was use explode("\n", $file); to seperate the lines, other than that, I have no clue how to split it again, nor organize it in an efficient way to make it into a neat table. – Bren Aug 06 '12 at 07:28
  • if you know how to iterate over a loop (`foreach`), then you can do this on your own if you think about it :) – JamesHalsall Aug 06 '12 at 07:28
  • I actually did use a foreach loop and I was not getting the outcome of what I was expecting. I have been sitting here for 4 and a half hours on something this simple and just can't seem to get it.. The more I continue to alter my code, the messier it gets and I've started all over from scratch multiple times within the time I've spent tinkering with this code. – Bren Aug 06 '12 at 07:31
  • It *looks like* CSV data, have a read of the [`fgetcsv()`](http://php.net/fgetcsv) documentation. – salathe Aug 06 '12 at 07:54

3 Answers3

1

You can do following :

$filename = 'abc.txt';
$content = file_get_contents($filename);
$explodedByBr = explode('<br/>', $content);
$table = "<table border='1'>";
foreach ($explodedByBr as $brExplode) {
  $explodedByComma = explode(',', $brExplode);

  $table .= "<tr>";
  foreach ($explodedByComma as $commaExploded) {
    $table .= "<td>" .$commaExploded. "</td>";
  }
  $table .= "<tr/>";
}
$table .= "</table>";

echo $table;

abc.txt has data in following format :

3434,83,8732722
834,93,4983293
9438,43933,34983

Atul Rai
  • 242
  • 2
  • 10
0
<?php
    $file = 'path/to/file.txt';
    echo '<table>';
    while(!feof($file)) {
        $line = fgets($file);

        echo '<tr><td>' . implode('</td><td>',explode(',',$line)) . '</td></tr>';
    }
    echo '</table>';
?>
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
0

Try this:

Read the file into an array and then column'ize it by processing each line of the array by passing it through array_walk.

<?php
function addElements( &$v, $k ) {
    $v1 = explode( ',', $v ); // break it into array
    $v2 = '';
    foreach( $v1 as $element ) {
        $v2 .= '<td>'.$element.'</td>';
            // convert each comma separated value into a column
    }
    $v = '<tr>'.$v2.'</tr>'; // add these columns to a row and return
}

// read the whole file into an array using php's file method.
$file = file( '1.txt' );
// now parse each line of the array so that we convert each line into 3 columns.
// For this, i use array_walk function which calls a function, addElements, 
// in this case to process each element in the array.
array_walk( $file, 'addElements' );
?>
<html>
     <head></head>
     <body>
         <table border="0">
             <?php echo implode('',$file);?>
         </table>
     </body>
</html>

Hope it helps. See the php doc for file and array_walk. These are simple and convenient functions.

web-nomad
  • 6,003
  • 3
  • 34
  • 49