reading a TXT file into variables

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
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

reading a TXT file into variables

Post by leewad »

Hi

I have the following format in a txt file

[text]EURO
1.1232
1.2836
US DOLLARS
1.4468
1.6485
AUSTRALIAN DOLLARS
1.4707
1.6913
BAHRAINIAN DINARS
0.5467
0.6283
BARBADOS DOLLARS
2.7550
3.2969
BRAZILIAN REAL
2.7477
3.4578
BRUNEI RINGGITS
1.8288
2.1017
BULGARIAN LEV
2.0866
2.5752[/text]

Im wanting read it in php and set variables to them i.e

Code: Select all

$currency = $line1;
$rate_buy= $line2;
$rate_sell= $line3;
using this for each currencies

Could anybody help me out please
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: reading a TXT file into variables

Post by requinix »

Have you tried your hand at a fopen/fgets/fclose loop? There are examples in the documentation for those functions.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: reading a TXT file into variables

Post by litebearer »

Perhaps...

Code: Select all

<?php
$lines = file("data.txt");
$x = 0;
$c = count($lines);
while($x<$c) {	
	echo "Currency: " . $lines[$x] . " -  Buy: " . $lines[$x+1] . " - Sell: " . $lines[$x+2] . "<br/>"; 
	$x = $x + 3;
}
?>
example: http://nstoia.com/sat/data.php
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Re: reading a TXT file into variables

Post by leewad »

Thanks a lot will give it a go :-)
Post Reply