Forum Moderators: coopster

Message Too Old, No Replies

Parsing a TXT file

         

Courtman

3:05 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



Hi there,

I am trying to convert a text file [weather.noaa.gov...] into the welcome page of my website (which is currently .htm). I'd like to strip the date/time line off and just leave the second line on, and then alter the font to be the same as my site.

Does anyone know how I can do this?

Thanks in advance!

[edited by: jatar_k at 5:18 pm (utc) on April 27, 2004]
[edit reason] linked up url [/edit]

barn_de

3:32 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



Hi Courtman,

i'm sure there is a better solution, but this works so far:

<?

$fp = fopen('http://weather.noaa.gov/pub/data/observations/metar/stations/EGGW.TXT', 'r');

while(!feof($fp)) {
$sContent .= fgets($fp, 4096);
}
fclose($fp);

$aLines = explode("\n", $sContent);

echo '<font face="verdana">' . $aLines[1] . '</font>';

?>

coopster

3:38 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Another option is PHP's file function...
$lines = file [php.net]('http://weather.noaa.gov/pub/data/observations/metar/stations/EGGW.TXT'); 
print $lines[1];

Netizen

3:43 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



You could do

$dataArray=file('http://weather.noaa.gov/pub/data/observations/metar/stations/EGGW.TXT');

and then print $dataArray[1] within your page in the appropriate place.

Courtman

6:17 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



barn_de,

Thanks - that works fine. One more question, is there a way to strip the first 5 characters from the line as well... It will make organising the output on the page much easier.

Thanks for your help in advance, I've been going round in circles on this for days!

Netizen

6:35 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



If you want to strip the first 5 characters then take a look at substr [php.net].

$newString=substr($oldString,5);

Hope that helps.

Courtman

9:07 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



Perfect, it all works a treat! Many thanks for your help!

Courtman

12:14 pm on Apr 28, 2004 (gmt 0)

10+ Year Member



One more question, I've got another file which may be multiple lines [weather.noaa.gov ]. Again I need to strip the first line, then leave the rest in with the line breaks.... is this done the same way or is there a more complex parse method?

Thanks for your help on this, I'm very new to PHP but am slowly coming to terms with its power!

Courtman

12:25 pm on Apr 28, 2004 (gmt 0)

10+ Year Member



Better still, can I remove the line breaks in the code after stripping the first line and just leave it as one line of code which will then be displayed on the page...

mykel79

2:25 pm on Apr 28, 2004 (gmt 0)

10+ Year Member



The file() function reads the whole file into an array, one line at a time. So you can still use the same parse method. If you want to strip the line breaks, once again you can use the the str_replace function.
str_replace("\n","",$variable)

For each line before printing it out.

Netizen

8:37 pm on Apr 28, 2004 (gmt 0)

10+ Year Member



I would do this like:

$dataArray=file('http://weather.noaa.gov/pub/data/forecasts/shorttaf/stations/EGGW.TXT');

$lineToShow=implode(' ',array_slice [php.net]($dataArray,1));

which gives you all the lines in the array apart from the first one and join the lines together with a space.