• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

read line by line a file?

i dont do much file handling and was wondering if anyone could tell me how to read a line by line of a file then
split it up with TABS....
i know to use the explode function to split just unsure how tab delimated are handled
0
aot2002
Asked:
aot2002
1 Solution
 
Diablo84Commented:
You can read a file into a string using the file_get_contents function, eg:

$myfile = file_get_contents('thefile.extension');

You could alternatively use the file function if you wanted to load the file into an array (1 line per index):

$myfile = file('thefile.extension');

You can then use explode and \t to split the string into an array, eg:

$parts = explode("\t",$myfile);

Diablo84
0
 
Diablo84Commented:
... note that if your file also contains new lines and other white space you might be better off working with a regular expression, eg:

<?php
$myfile = file_get_contents('filename.txt');
$parts = preg_split("/[\s]+/",$myfile);

print_r($parts);
?>

Diablo84
0
 
donelCommented:
        Please Try the Below

         $filename="pathtoyourfile";

         $handle = fopen ($filename, "r");
      $result="";
      // throw the data into our result
      while (!feof($handle))
             {
              $row_data= fgets($handle, 1000000);
             $result[]=explode('\t',$row_data);
              }
      fclose ($handle);

         I am not sure if it is the one you are looking for.

        thanks!
        bj mac donel
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
aot2002Author Commented:
this worked but you forgot that it returned an array !


      $myfile_array = file($_FILES['IIFFile']['tmp_name']);
      foreach ($myfile_array as $line_num => $myfile) {
  echo $line_num." ".$myfile."<br><br>";
  $parts = explode("\t",$myfile);
for($a=0; $a < count($parts); $a++){
            echo $parts[$a]."|";
            }
            
      }
      }
0
 
aot2002Author Commented:
just wondering if you work as a php programmer for a living diablo84 ?
0
 
Diablo84Commented:
I didn't forget, i just wasn't expecting you to use that method :)

Depending on the structure of the file you are working with you *may* be able to work with the following to simplify your code:

$myfile_array = file($_FILES['IIFFile']['tmp_name']);

foreach ($myfile_array as $line_num => $myfile) {
 echo "$line_num - ";
 echo preg_replace('/[\s]+/','|',trim($myfile)).'<br>';
}

>>  just wondering if you work as a php programmer for a living diablo84 ?

Just lately i have mainly been working Freelance on the PHP side of things and working with a friend to help get his Web Development business on the road.

Diablo84
0
 
aot2002Author Commented:
nice thanks im working with IIF quickbooks file import to mysql
0
 
majestiqCommented:
you might be better off using the fgetcsv function.  For example..

$fp = fopen("myfile.txt", "r");

while($fp && !feof($fp))
{
   $line = fgetcsv($fp, 1024, "\t");
   print_r($line);
}
fclose($fp);

The fgetcsv returns an array from the file and handles some of the fields being surrounded by quotes.  This function is meant to be used with comma and tab delimited fiels.

Khurram Khan
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now