[php] Search .txt document and return values into a variable? show me

A user will submit a value on my form.

Say AB10. That goes into a variable called $code1

Then I need to search this big long text document to find the values to code1 and return them.

Here is the structure of the text document.

AB10,392900,804900,57.135,-2.117
AB11,394500,805300,57.138,-2.092

I then need to find AB10 and return the first bit after the comma "392900" and put that into a variable called $variableX. Then I need to take the next value after the comma "804900" and put that into a variable called $variableY - I can disgard the rest of the data.

So how can I do that? I need to be able to pull the values and process them in the rest of  my script.

LVL 5
georgecooldudeAsked:
Who is Participating?
 
lubovecConnect With a Mentor Commented:
and replace line

} while ((!feof ($handle)) || (!$x));

by

} while ((!feof ($handle)) || ($x == ""));
0
 
php-webdesignCommented:
does it need to be a txt document? database is easier :)
0
 
georgecooldudeAuthor Commented:
Yes I afraid so. :-)

Later in the future I will be using a database but for now it has to be a .txt

It weights in at around 90KB!  


0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
lubovecCommented:

try this code, read file and compare first 4 chars in ever line with suplied string. if strcmp return 0, strings are the same... then split line by comma character

$handle = fopen ("/tmp/yourfile.txt", "r");
do {
   $buffer = fgets($handle, 4096);
   if (strcmp(substr($buffer, 0, 4),$code1) == 0) list($s, $x, $y, $next1, $next2) = split(',', $buffer);
} while ((!feof ($handle)) || ($end))
fclose ($handle);

note: not tested, only used www.php.net ;o)
0
 
georgecooldudeAuthor Commented:
lubovec,

Some bits I get some I do. Would you be so kind as to run over them for me and also point me in the direction you found this code of the php.net website?

$handle = fopen ("locationList.txt", "r"); //Understand


do {
   $buffer = fgets($handle, 4096); // Is 4096 ammount of memory allowed???

   if (strcmp(substr($buffer, 0, 4),$code1) == 0) list($s, $x, $y, $next1, $next2) = split(',', $buffer); //Could you explain this one. How does it search for $code1 in the list?


} while ((!feof ($handle)) || ($end)) //Dont understand this line



fclose ($handle); //Understand


If you could go over those it would be great. Also php-webdesign I am interested to hear your solution too! :)
0
 
php-webdesignCommented:
i know... and i came up with the same solution as given above :)

BUT

try following articles:

http://codewalkers.com/tutorials/57/1.html
http://php-scripts.com/php_diary/010800.php3
0
 
georgecooldudeAuthor Commented:
I also get this error for your code lubovec

Parse error: parse error, expecting `';'' - for the line of:

fclose ($handle);
0
 
lubovecCommented:

I'm sorry for errors I made in my comment

1.
use searching in www.php.net

2.
repaired code:

$handle = fopen ("locationList.txt", "r");

do {
   $buffer = fgets($handle);
   // second parameter define number of chars that will be read out
   // http://sk2.php.net/manual/en/function.fgets.php

   if (strcmp(substr($buffer, 0, 4),$code1) == 0)
   // strcmp compare 2 strings, substr find substring in suplied string, second is $code1 from user input
   // http://sk2.php.net/manual/en/function.strcmp.php
   // http://sk2.php.net/manual/en/function.substr.php

      list($s, $x, $y, $next1, $next2) = split(',', $buffer);
   // if they are the same, feed list($s, $x, $y, $next1, $next2) by result of function split
   // http://sk2.php.net/manual/en/function.split.php
   // http://sk2.php.net/manual/en/function.list.php

} while ((!feof ($handle)) || (!$x))
   // loop ends when eof is reached or $x contains value

fclose ($handle);

3.
another solution is use of ereg() or eregi(), http://php-scripts.com/php_diary/010800.php3 from php-webdesign

i hope now it is clear :)

bye

0
 
georgecooldudeAuthor Commented:
lubovec,

I don't know why but i just copyed your exact code into my editor and I still get the same error.

Parse error: parse error, expecting `';''    for the line of:

fclose ($handle);


0
 
lubovecCommented:
hehe, sorry, ; is missing after while...

this line
} while ((!feof ($handle)) || (!$x));

it's only syntax
0
 
georgecooldudeAuthor Commented:
ok thanks.

I corrected that line:

} while ((!feof ($handle)) || (!$x));

But now I get "Undefined variable: x" for the above line.

What should i change? hehe, thanks for helping me so far. I am quite new to this
0
 
php-webdesignCommented:
changer error_reporting(E_ALL);

to

error_reporting(E_ALL ^ E_NOTICE);
0
 
georgecooldudeAuthor Commented:
locationList checks through around 3000 lines so I keep getting timeouts.

I would like to know how to fix this so my script doesn't time out :-)
0
 
php-webdesignCommented:
time out period is set server side...

so you need to edit your apache (if you run apache) config file
0
 
georgecooldudeAuthor Commented:
php-webdesign,

I think the code itself has problems.

Everytime I run it it goes into an endless loop and totally crashed out my machine. Ive tryed a seperate box and had the same result. I even change the locationList.txt file to include just 1 line and I still got an endless looping crash.

Are you sure the code is good? Surely if I've got an undefined variable it needs addressing?
0
 
php-webdesignCommented:
not sure if your code is right, since i've not posted it...

will take a look at it tomorrow... going home now ;)
0
 
lubovecCommented:

add line
$x = "";
at begin of your script

} while ((!feof ($handle)) || ($x == ""));

OR try to write script with database right now, if you can... there is more easily and you can save the time ;)
0
 
georgecooldudeAuthor Commented:
I have found a solution now and my script is working
0
All Courses

From novice to tech pro — start learning today.