Jump to content

Need Help Please - Searching and Extracting from Text File


Looktrne

Recommended Posts

I have a large text file that I need to search and extract text from.

 

I have some code that somewhat works but is not good for what I need because it only reads one line at a time.

 

I need to be able to echo all code between two strings and continue scanning the entire document.

 

I am attaching the TXT file that is being read by the script:

 

Here is the script:

 

<?
$searchthis = "Problem:";
$search="Check:";
$matches = array();

$handle = @fopen("1numbers.txt", "r")or die("can't open file");

if ($handle)
{
    while (!feof($handle))
    {
        $buffer = fgets($handle);
        if(strpos($buffer, $searchthis) !== FALSE)
            echo "<br>". $buffer."<br>";
		     if(strpos($buffer, $search) !== FALSE)
            echo "<br>". $buffer."<br>";			
    }
    fclose($handle);
}
?>

 

you can see what this script outputs by visiting this link:

 

http://yourautofix.com/data/data.php

 

but my problem is it only outputs one line of text that finds the search match.

 

I need it to output all lines of text between two matches for example any text between "Problem:" and "Check:" should be Echo'd

 

and any text between "Check:" and "Likely:" should be echo'd

 

there may be 1 line or 20 lines of text between the tags... I need to print all lines between the 2 determined search strings and then continue through the text file displaying all matches between the search strings in a  large file.

 

any thoughts on how I can get this done or point me in the right direction?

 

Thanks for any input on this

 

Paul

17752_.txt

Link to comment
Share on other sites

After a search hit you need to set a state variable that you then test for to see if further lines should be output for each loop.  This variable should be cleared when you reach the end of a group of lines (the next Check or Likely?).

 

While the state variable is set you echo all lines.

 

You might run into the problem of reading too much, but the above is principally what you need to do.

Link to comment
Share on other sites

The principle:

 

$echo = false;

while (!feof($handle))

{

    $buffer = fgets($handle);

 

    if (found start pattern)

        $echo = true;

 

    if (found end pattern or a new start pattern?)

        $echo = false;

 

    if ($echo)

        echo $buffer;

}

 

You know better than me what determines the end of the sequence.

 

Cheers,

Anders

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.