Jump to content

Read file


greenba

Recommended Posts

Hello,

 

I have the following in a file:

   

    1 2008-10-28 16:20:01 1 0 1 0

    2 2008-10-28 16:20:16 1 0 1 0

    1 2008-10-29 06:37:09 1 0 1 0

  11 2008-10-29 07:06:58 1 0 1 0

  44 2008-10-29 07:07:12 1 0 1 0

    2 2008-10-29 07:10:06 1 0 1 0

    3 2008-10-29 13:30:05 1 1 1 0

  15 2008-10-29 13:30:10 1 1 1 0

  24 2008-10-29 13:30:14 1 1 1 0

    3 2008-10-29 13:30:18 1 1 1 0

  15 2008-10-29 13:30:23 1 1 1 0

 

I need to read it line by line and separate each row into 6 variables, so that for the first row;

$var1 = 1

$var2 = 2008-10-28 16:20:01

$var3 = 1

$var4 = 0

$var5 = 1

$var6 = 0

and so on.

 

I have attached a file on how it exactly looks like.

 

Any ideas on how to do this? All help is appreciated

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/133775-read-file/
Share on other sites

I prefer file it puts each line into an array. Keep in mind though when you explode, the date and time will be separate elements since there is a space between them.  To get your $var2 you're going to have to glue those 2 elements back together.  Simple string concat will do the trick.

Link to comment
https://forums.phpfreaks.com/topic/133775-read-file/#findComment-696188
Share on other sites

Or since the file is tab delimited -

 

<?php
$file = "sample.txt";
$lines = file($file);
foreach($lines as $line){
$data = explode("\t",$line);
$data = array_map('trim', $data); // trim any extra tabs/newlines
echo "<pre>",print_r($data,true),"</pre>"; // here is what your data for each line looks like
// put your code here to process the data in each line
}
?>

Link to comment
https://forums.phpfreaks.com/topic/133775-read-file/#findComment-696194
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.