Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does one "search" a txt file?

Status
Not open for further replies.

IamStang

Technical User
Feb 12, 2005
27
0
0
US
I know it is possible but I am finding very little information pertaining to my question.

Everything tells one how to read/write/append to a txt file. I dont know, maybe I am just not looking in the right places.

What I am trying to accomplish is this: I have a txt file. It is formatted something like this.

000001*something1*something2*something3
000011*something4*something5*something6
000111*something7*something8*something9

Using a link such as "mypage.php?id=000011" should return something4, something5 & something6.

As you have probably figured out already, I am just starting in to php scripting.

If anyone would care to point me in the right direction with this, either by way of an online tutorial/information or an example, it would be much appreciated.

Thanks!
IamStang
 
In order to search the content of a file you have to read the file into memory using either fopen() and fread() or file_get_contents().
The content can then be searched using string functions (if the content was read into a string). Reading a file into an array is also possible, but makes the 'search' a bit more involved since multiple array functions are needed.
So, have a look at the FILE section in the PHP manual.

Your search will probably involve the use of some regular expressions to locate the correct section and extract the information. If you need help with that, just let us know.
I also thought of the possibility of using explode(), but for that you had to iterate the lines of the file until you reach the correct entry.

This solution is only workable for small files. If you have more data you should consider the use of a database.
 
Thanks for the reply.

The file I am working with should never be more than 20 or so lines, so I dont believe size will be an issue.

And, as it always seems to work out, I have found information, just a few minutes ago, regarding this. It never fails. I spend days trying to figure it out on my own. Give up and post to a board such as this one. And the next thing I know, it's staring me in the face. LOL.

Thanks again!
IamStang
 
Here's a my quick solution using the fgetcsv() function:
Code:
<?
$fp = fopen('testcsv.dat','r');
$txt = array();
while (($data = fgetcsv($fp,1000,'*')) !== FALSE) {
   $num = count($data);
   for ($i=1;$i<$num;$i++)
        $txt[$data[0]][] = $data[$i]; }
fclose($fp);
if (isset($_GET['id']))
    echo 'Search for ' . $_GET['id'] . ' finds: ', implode(', ',$txt[$_GET['id']]);
?>
The output using your example data is:
Code:
Search for 000011 finds: something4, something5, something6

Ken
 
It would probably be a lot easier to use a database for this (as well as producing much more readable code), though that depends on you having access to one of course.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top