Solved

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

Posted on 2004-10-04
18
263 Views
Last Modified: 2010-04-17
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.

0
Comment
Question by:georgecooldude
  • 8
  • 5
  • 5
18 Comments
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12216163
does it need to be a txt document? database is easier :)
0
 
LVL 5

Author Comment

by:georgecooldude
ID: 12216249
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
 
LVL 1

Expert Comment

by:lubovec
ID: 12216268

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:georgecooldude
ID: 12216815
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
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12216859
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
 
LVL 5

Author Comment

by:georgecooldude
ID: 12217010
I also get this error for your code lubovec

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

fclose ($handle);
0
 
LVL 1

Expert Comment

by:lubovec
ID: 12217206

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
 
LVL 5

Author Comment

by:georgecooldude
ID: 12217362
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
 
LVL 1

Expert Comment

by:lubovec
ID: 12217453
hehe, sorry, ; is missing after while...

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

it's only syntax
0
 
LVL 5

Author Comment

by:georgecooldude
ID: 12217597
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
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12217630
changer error_reporting(E_ALL);

to

error_reporting(E_ALL ^ E_NOTICE);
0
 
LVL 5

Author Comment

by:georgecooldude
ID: 12217917
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
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12217945
time out period is set server side...

so you need to edit your apache (if you run apache) config file
0
 
LVL 5

Author Comment

by:georgecooldude
ID: 12218302
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
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12218540
not sure if your code is right, since i've not posted it...

will take a look at it tomorrow... going home now ;)
0
 
LVL 1

Expert Comment

by:lubovec
ID: 12219971

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
 
LVL 1

Accepted Solution

by:
lubovec earned 30 total points
ID: 12219987
and replace line

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

by

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

Author Comment

by:georgecooldude
ID: 12306937
I have found a solution now and my script is working
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Image decoding from Camera 3 96
Please explain: Aspect Oriented Programming 2 100
C# code editing and collaboration 3 131
collection output issue 9 36
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question