Jump to content

PHP Find line in text file and show following 3 lines


kerrodhall

Recommended Posts

I have set up a cron job through cPanel which downloads a txt file via ftp from the following address, ftp://ftp2.bom.gov.au/anon/gen/fwo/IDQ10050.txt, to my server.

 

I want to be able to search for the text "IDQ1005008" (without the "" marks) and then display the next few lines.

 

e.g.:

 

EXRACT OF TEXT FILE:

====================================================

IDQ1005007

HERBERT AND LOWER BURDEKIN DISTRICT

Fine. Light to moderate E to NE winds. A moderate to high fire danger. 

Outlook for Monday    ... Fine.

Outlook for Tuesday    ... Isolated showers or storms inland.

 

IDQ1005008

TOWNSVILLE

Fine. Light to moderate NE winds.   

MAX    32

UV INDEX - 14 [Extreme] UV Alert from  8:00 to 16:10

Outlook for Monday    ... Fine.

====================================================

Search the file for the phrase IDQ1005008 (which indicates which weather district);

 

Now I want to be able to display the 5 lines under IDQ1005008 (echo or whatever other command) as they are shown above (with the line breaks)

 

Does anyone know how to do it? Could you please paste the code here as the last forum I posted on sent me psuedocode and I am not good with that sought of thing, I want an exact solution if possible.

 

Thanks in advance.

Link to comment
Share on other sites

This is the code I have so far... It is not complete as it does not remove any of the other text...

<?

$findThis = "IDQ1005008";

$handle = @fopen("IDQ10050.txt", "r"); // Open file form read.

 

if ($handle) {

while (!feof($handle)) // Loop til end of file.

{

$buffer = fgets($handle, 4096); // Read a line.

if ($buffer <> "IDQ1005008") // Check for string.

{

echo "$buffer"; // If not string show contents.

} else {

$buffer = $findThis; // If string, change it.

echo "$buffer"; // and show changed contents.

}

}

fclose($handle); // Close the file.

}

?>

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.