Solved

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

Posted on 2004-10-04
18
261 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual xHarbour 1 76
strCopies  challenge 17 73
create an incrementing variable name AutoHotKey 5 57
wordmultiple challenge 12 90
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now