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.

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.

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.