Solved

Searching large text files

Posted on 2002-03-03
6
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 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