Solved

Searching large text files

Posted on 2002-03-03
6
260 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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