PHP Programming > Code Snippets

Help with get function

(1/2) > >>

Bojmaliev:
Hello,
Today i need php help,
how i can read something by file
Ex.
 
File.php or file.ini or file.sav
It looks like this:
time=10min
gang=1
ip=IP adreass
bank=323232
money=23232
 
From this file i want get something after  "="
 
ex. $GetPlayerInfo['gang']  and it gets "1"
ex. $GetPlayerInfo['bank'] and it gets "1"
 
Thanks, :)

ErnieAlex:
Well, if it is not a huge text file, you can read it into an array.  Each line would be a different entry in the array.
Then, loop thru each array entry and parse each line.  You can separate the values by splitting values in each
line into "key" and "value" and then use the results.  To do this, you would use the "explode" command.
Not too hard to do.
  Something like:  $parms=explode("=",$line)   which would put left of "=" into $parms[0] and the stuff on the right side of "=" into $parms[1]  ...   Where $line is the full line you got from the file!

  You could do this in just a few lines... 
     1) read entire file into a variable
     2) parse the entire file into an array of lines (Hint: use explode and explode on $VBCRLF)
     3) loop thru and parse each line into your array of data

Hope all that makes sense!  Good luck...

Bojmaliev:
Thanks, i will try, if i have more questions or problems i will post here :) , thanks again

sergo_bero:
try like this


--- PHP Code: ---

$input = fopen('file.txt','r');
$array = Array();

while(!feof($input)) {

$line = trim(fgets($input));
$pieces = explode('=',$line);
$array[$pieces[0]] = $pieces[1];

}

--- End code ---


then you have array with key => value

try
print_r($array)
and take a look...

ErnieAlex:
Yes, Sero, I told him that, but, was letting him figure out his own version of the code.
This site has a large number of students looking to finish their homework with.

Navigation

[0] Message Index

[#] Next page

Go to full version