Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Searching large text files

Posted on 2002-03-03
6
Medium Priority
?
277 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 900 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

916 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