Thursday, 3 December 2015

File Reading and Tockeniser in PHP

Hi Friends,
Reading a file using PHP is pretty easy. Here is the code which read a text file and tockenize that line into words and displaying them in a formatted table format.

So we need two files. One is the text file (HUPFilestructure.txt) and another PHP file to process the same.

1. HUPFilestructure .txt

HUP HUP HUPA
HUPA HUP TFTKTU
TFTKTU HUP HUPA

2. PHP file (fileprocess.php)

<?php
$myfiles = fopen("HUPFileStructure.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
echo "<table border=1>";
while(!feof($myfiles))
{
  $row= fgets($myfiles); // Read line by line
 
  $tok = strtok($row, " "); // Tockenize that line using space
  echo "<tr>";
  while ($tok !== false)
  {
      echo "<td>$tok</td>";
    $tok = strtok(" ");
  }
  echo "</tr>";
}
echo "</table>";
fclose($myfiles);
?>

No comments:

Post a Comment