reading from files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

reading from files

Post by dannyd »

Im trying to read a space delimted .txt file that looks like this:

1 060000 000200 XYZ-3052
3 060200 000015 XYZ-0132
5 060215 000015 XYZ-2724
7 060230 002829 CDY-2909

How would I loop through the file line by line and insert each element into a field that has 4 columns for each element ?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: reading from files

Post by Zoxive »

Code: Select all

$Lines = explode("\n",$File);
// Explodes by every line (\n)
 
foreach($Lines as $Line){
  list($One,$Two,$Three,$Four) = explode(' ',$Line);
  // Col One,Two,Three,Four
}
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: reading from files

Post by dannyd »

That sounds like what I need, how do I output the first column ?

I thought it might be something like

echo $Line[$One]; //doesnt work.

What im trying to do is identify the columns as well as the associated elements with a column within the file. Im trying to eventually store the elements into a database.

Thanks for the help!
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: reading from files

Post by Zoxive »

Just $One would work.

The output of explode is an array, and i have list convert the array to 3 variables.

You could remove the list and use $Line[0], $Line[1], and $Line[2] (Arrays start at 0)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: reading from files

Post by Christopher »

dannyd wrote:echo $Line[$One]; //doesnt work.
$Line is exploded into an array and assigned to a list of variables in the example.
(#10850)
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: reading from files

Post by dannyd »

Heres a sample line of the .txt file. I changed it to have comma delimiters.

06:00:00:00,2/12/2008,COM-3474,CD COMPILATION,A367,00:02:00:00
07:00:00:00,2/12/2008,COM-3474,SAMMY PRO,A367,00:02:00:00
08:00:00:00,2/12/2008,COM-3474,THE SHOVEL PRO,A367,00:02:00:00


I tried the code below:

<?PHP

$file_handle = fopen("02-12-2008.txt", "rb");

$Lines = explode("\n",$file_handle);
// Explodes by every line (\n)

foreach($Lines as $Line){

list($One,$Two,$Three,$Four) = explode(',',$Line);
// Col One,Two,Three,Four

echo $One . '<BR>';
echo $Line[0];

}
?>


And the output I recieve is:

Resource id #2
R



Any ideas what I could be doing wrong ?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: reading from files

Post by Zoxive »

$Line = explode(',',$Line);

If you just want to use $Line[0], $Line[1] .. etc

Edit:
But, the problem is you are using fopen, simplestway is file_get_contents()

IF you want to use fopen, then you need to read it differently.

fgets();
fread();
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: reading from files

Post by dannyd »

Where do I insert that line below:

$Line = explode(',',$Line);

I tried this which im sure doesnt require two explode's:

<?PHP

$file_handle = fopen("02-12-2008.txt", "rb");

$Lines = explode("\n",$file_handle);
// Explodes by every line (\n)

foreach($Lines as $Line){

list($One,$Two,$Three,$Four) = explode(',',$Line);
// Col One,Two,Three,Four

$Line = explode(',',$Line);

echo $Line[0];

}

?>


And got output:

Resource id #2
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: reading from files

Post by Christopher »

Please put code tags around your code.

You need to keep track of your variables -- which are strings and which are arrays.

Code: Select all

$file_handle = fopen("02-12-2008.txt", "rb");
 
 // Explodes doc into lines (\n)
  $Lines = explode("\n",$file_handle);
  
 foreach($Lines as $Line){
     
  $fields = explode(',',$Line);
 
  echo $fields[0];
  
  }
(#10850)
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: reading from files

Post by dannyd »

That doesnt work either:

<?PHP

$file_handle = fopen("070202.txt", "r");

// Explodes doc into lines (\n)
$Lines = explode("\n",$file_handle);

foreach($Lines as $Line){

$fields = explode(',',$Line);

echo $fields[0];

}
?>


The .txt file has 1 line:
1,060000,000230,COM-3211

Output:

Resource id #2
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: reading from files

Post by Zoxive »

Its resource because you are using fopen. Look at my previous post.

A simple fix is below.

Code: Select all

$file_handle = fopen("070202.txt", "r");
// Change to this
$file_handle = file_get_contents('070202.txt');
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: reading from files

Post by dannyd »

Thanks for the help!

It seems to work fine with comma delimited file. How do you handle a space delimiter that can vary in size. For instance if the .txt file rows looks like this:

1 10 spaces Dan 5 spaces 29 4 spaces Canada
2 10 spaces Mike 5 spaces 19 4 spaces USA
3 10 spaces George 5 spaces 25 4 spaces Australia

I actually have to work with a file thats space delimited but the spaces vary in size.
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: reading from files

Post by dannyd »

Heres the thing when I import the .txt file into excel and selecting space as the delimiter it formats the data into columns correctly.

However when I specify the delimiter as " " in PHP it doesnt work. The spaces vary between the columns. What delimiter do I use when the spaces between the columns may vary ?

Thanks for the help ?
Post Reply