Solved

Searching large text files

Posted on 2002-03-03
6
255 Views
Last Modified: 2008-03-17
Hey, I have a large text file that I'd like to perform a search function on.  The file will be around 3 + mb big.  I want the search the return the line that the variable resides on.  Here's a small example:

text.txt
--------
line 1 - stuff
line 2 - things, other, misc
line 3 - that this the other
line 4 - this him her

Searching for 'things' will return: 'line 2 - things, other, misc'.
Searching for 'this' will return: 'line 3 - that this the other' & 'line 4 - this him her'.

I tried opening the file & returning the data into an array splitting it by "\n":

search.php
----------
$fulldir = "/myhost/";
$fullpath = $fulldir . "text.txt";

$fp = fopen($fullpath, "r");
$fdata = fread($fp, filesize($fullpath));
fclose($fp);

echo $fdata;

$mlist[] = split("\n",$fdata);

$result = array_search($search,$mlist[],true);

if($result != $null){ print $mlist[$result]; };

 When running the file the server would Hang Up on me.  I'm guessing this is a large operation to run & the server can't handle it?  I'm wondering if anyone can produce a code to search the file & return specific lines that the text matches.  Hopefully a script that doesn't eat the server usage.

It seems to me that opening the file is a big task, if there's another way to scan the file without opening I'd be interested to know.

Thanks
Gunner
0
Comment
Question by:Helpmeplease
  • 4
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
andriv earned 300 total points
ID: 6837918
The file() function returns an entire file as an array. Each line is a seperate alement of the array. Now you do not need the extra code to make it an array and may be easier on your server.

$mlist = file($fullpath);

$result = array_search($search,$mlist[],true);

if($result != $null){ print $mlist[$result]; };

If the file is as big as you say no matter what you do will work the server but you can reduce it my reducing the number of things the server has to process.

0
 

Author Comment

by:Helpmeplease
ID: 6838068
I used the file() command & it splits the array great.  Problem is that array_search doesn't search how I explained above.

Put simple - I want a way to check each line to see if "string_a" exists in that line - if it does print the line.

here's some examples but I'd rather not use them:

working example

$x = "1";
$max = count($mlist);
while ($x <= $max) {
  // if ($variable isin $mlist[$x]) { print $mlist[$x]; }
}

the above is IRC script language but I aren't sure how to do it on PHP.  Please help
0
 

Author Comment

by:Helpmeplease
ID: 6838093
ahh it's ok
I managed to sort the rest myself - didn't really need much help after all.  :)

I'll give ya the points anyway.  Look out for another 300 pointer when I get to the next stage. :)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Helpmeplease
ID: 6838096
here's my final script:

<?php

set_time_limit(60);

$path = pathinfo($PATH_TRANSLATED);
$fulldir = $path["dirname"];
$fullpath = $fulldir . "/masterlist.txt";

$mlist = file($fullpath);

for($index=0; $index < count($mlist); $index++){
if (eregi($search, $mlist[$index])) {
$results[] .= $mlist[$index];
}}
$total = count($results);
echo "$total results found<br>";
$x = "0";
$max = count($results);

while($x <= $max){
  echo "$results[$x]<br>";
$x++;
}

?>

0
 

Author Comment

by:Helpmeplease
ID: 6838097
Thanks for help, pointed out one thing I needed :)
0
 
LVL 5

Expert Comment

by:andriv
ID: 6838295
No problem, glad I was able to help!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 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

22 Experts available now in Live!

Get 1:1 Help Now