Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

reading files, splitting lines

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

reading files, splitting lines

Postby robbied » Wed Feb 19, 2003 8:51 am

So I have been a Perl guy for 3 years (please limit the Boo-ing) :)

I really dig php, it is really powerful and I can see using it more than Perl in my development.

I am having real issues with Perl-isms and trying to do them in PHP.

I live at php.net and can seem to figure this one out.

//

I want to read a file, line by line, and the split each line into 5 variables.

Pretty simple eh ?

In Perl it would be flippin' simple

Code: Select all
#sorry to insult you with perl code on a php forum

$file = "foo.txt";
open(FH, $file);
# read each line of the file into an array element
@array = <FH>;
close(FH);

foreach $elt (@array) {
# remove newline from end of string
chomp($elt);
# split on each tab character
($var1, $var2, $var3, $var4, $var5) = split/\t/,$elt;
}



Why does all the php code I find seem to be 5 times larger than this code and 10 times more confusing ? I am obviously not looking in the right places, am I ?

Help !

Thanks
RobbieD 8O
User avatar
robbied
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Feb 19, 2003 8:35 am
Location: Victoria, BC

Postby pootergeist » Wed Feb 19, 2003 9:31 am

you'd either want to use fscanf() or fgetcsv() [I prefer fgetcsv]

usage

while ($usage = fscanf($fp,"%s\t%s\t%s\t%s\t%s\n")) {
list ($var1,$var2,$var3,$var4,$var5) = $usage;


while ($data = fgetcsv ($fp, 1000, "\t")) {
echo $data[0]; // etc etc
pootergeist
New php-forum User
New php-forum User
 
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK

Postby robbied » Wed Feb 19, 2003 3:58 pm

Thanks, the csv funct worked awesome, i just used tabs and it rocks !

Thank you very much

RobbieD
User avatar
robbied
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Feb 19, 2003 8:35 am
Location: Victoria, BC


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.