Page 1 of 1
reading from files
Posted: Wed Feb 13, 2008 3:13 pm
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 ?
Re: reading from files
Posted: Wed Feb 13, 2008 3:34 pm
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
}
Re: reading from files
Posted: Thu Feb 14, 2008 10:34 am
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!
Re: reading from files
Posted: Thu Feb 14, 2008 11:12 am
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)
Re: reading from files
Posted: Thu Feb 14, 2008 11:32 am
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.
Re: reading from files
Posted: Thu Feb 14, 2008 11:58 am
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 ?
Re: reading from files
Posted: Thu Feb 14, 2008 12:00 pm
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();
Re: reading from files
Posted: Thu Feb 14, 2008 12:08 pm
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
Re: reading from files
Posted: Thu Feb 14, 2008 12:53 pm
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];
}
Re: reading from files
Posted: Thu Feb 14, 2008 2:09 pm
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
Re: reading from files
Posted: Thu Feb 14, 2008 2:14 pm
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');
Re: reading from files
Posted: Thu Feb 14, 2008 2:33 pm
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.
Re: reading from files
Posted: Thu Feb 14, 2008 3:11 pm
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 ?