Jump to content

[SOLVED] I need help with parsing an HTML File.


aasmith26

Recommended Posts

:(  Hi You guys.  I am an intermediate PHP/MySQL Programmer.  I am trying to figure something out and I know you guys can help me.  I am trying to integrate weather warnings into my intranet portal website.  I can take care of the integrating by include(), but I need help with the actual warning script.  Usually I figure things out on my own, but this one is a doozie.  I have been searching since about 415 pm this evening.  Here's my code.

 

 

 

<HTML>

<HEAD>

</HEAD>

<BODY STYLE="background-color:transparent" topmargin=0 leftmargin=0>

<?php

 

// modify this value to change the NWS office used for warnings, to list ALL warnings use "ALL"

// to list warnings from a specific NWS office use the office name as it appears in the warning bulletins, ie:

 

"GOODLAND KS"

$nws_office = "ALL";

 

// modify this value to change the default (no warnings) text color

$default_fontcolor = "000000";

 

// modify this value to change the warning text color

$warning_fontcolor = "000000";

 

// modify this value to change the default (no warnings) background color

$bg_default_color="00FF00";

 

// modify this value to change the warning background color

$bg_warning_color="FF0000";

 

// modify this value to change the text box width

$width = 640;

 

// modify this text to change the text displayed when there are no warnings issued

$default_statement_string="There are no Severe Thunderstorm Warnings at this time";

 

// do not change anything below this line

 

$spotter_statement_string="<br>";

$space=" ";

$len=0;

 

$url1="http://www.nws.noaa.gov/view/validProds.php?prod=SVR&node=KBUF";

 

$nwsinfo = fopen($url1, "r");

 

if ($nwsinfo == TRUE)

{

while (!feof($nwsinfo))

{

$buffer = fgetss($nwsinfo, 1024);

 

if ($nws_office == "ALL")

{

if (ereg("SEVERE THUNDERSTORM WARNING FOR", $buffer))

{

$buffer = fgetss($nwsinfo, 1024);

$len = strlen($spotter_statement_string);

$spotter_statement_string = substr_replace($spotter_statement_string,"Severe Thunderstorm Warning for:<br>",$len);

 

while(1)

{

$len = strlen($spotter_statement_string);

$spotter_statement_string = substr_replace($spotter_statement_string,$buffer,$len);

$len = strlen($spotter_statement_string);

$spotter_statement_string = substr_replace($spotter_statement_string,"<br>",$len);

$buffer = fgetss($nwsinfo, 1024);

if (ereg("UNTIL", $buffer))

{

$len = strlen($spotter_statement_string);

$spotter_statement_string = substr_replace($spotter_statement_string,$buffer,$len);

$len = strlen($spotter_statement_string);

$spotter_statement_string =

 

substr_replace($spotter_statement_string,"<br>------------------------------------------------------------<br>",$len

 

);

break;

}

}

}

}

else

{

if (ereg($nws_office, $buffer))

{

while(1)

{

$buffer = fgetss($nwsinfo, 1024);

if (ereg("SEVERE THUNDERSTORM WARNING FOR", $buffer))

{

$buffer = fgetss($nwsinfo, 1024);

$len = strlen($spotter_statement_string);

$spotter_statement_string = substr_replace($spotter_statement_string,"Severe Thunderstorm Warning for:<br>",$len);

 

while(1)

{

$len = strlen($spotter_statement_string);

$spotter_statement_string = substr_replace($spotter_statement_string,$buffer,$len);

$len = strlen($spotter_statement_string);

$spotter_statement_string = substr_replace($spotter_statement_string,"<br>",$len);

$buffer = fgetss($nwsinfo, 1024);

if (ereg("UNTIL", $buffer))

{

$len = strlen($spotter_statement_string);

$spotter_statement_string = substr_replace($spotter_statement_string,$buffer,$len);

$len = strlen($spotter_statement_string);

$spotter_statement_string =

 

substr_replace($spotter_statement_string,"<br>------------------------------------------------------------<br>",$len

 

);

break;

}

}

break;

}

}

}

}

}

}

 

if ($spotter_statement_string == "<br>")

{

$fontcolor=$default_fontcolor;

$bgcolor=$bg_default_color;

$spotter_statement_string="<br> $default_statement_string";

}

else

{

$fontcolor=$warning_fontcolor;

$bgcolor=$bg_warning_color;

}

 

fclose($nwsinfo);

 

echo "<table border=\"2\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\" bgcolor=$bgcolor height=\"60\"

 

width=\"$width\">\n";

echo "<td valign=\"top\"><center><font

 

color=\"$fontcolor\">$space$spotter_statement_string$space</font></center></td>\n";

echo "</table>\n";

 

$handle = fopen("http://www.nws.noaa.gov/view/validProds.php?prod=SVR&node=KBUF", "r");

$contents = fread($handle, filesize($url1));

fclose($handle);

 

$array01 = explode("&", $contents);

 

echo $array01[0];

 

 

?>

 

</BODY>

</HTML>

 

That is exactly how the code is.  Here is what shows up.  http://andy1.servehttp.com:26/svr.php.  It only parses the file up to the line * UNTIL 1130 AM EST.  If you look at the actual warning page [as of 12/15/08, 930pm], http://www.nws.noaa.gov/view/validProds.php?prod=SVR&node=KBUF, it has a lot more data that I could use.  Now, I would like to start at the top of the file where it says 'THE NATIONAL WEATHER SERVICE IN BUFFALO  HAS ISSUED A'  and end at the $$ termination point.  I don't want anything but that text to show up.  Take a look at what I got and if you can help me it would be greatly appreciated.  Thanks.  I will help you guys as much as I can.

Link to comment
Share on other sites

<?php
function get($url){
$context = stream_context_create(
	array(
		'http' => array(
			'method'=>"GET"
		)
	)
);
return file_get_contents($url, null, $context);
}

$page = get('http://www.nws.noaa.gov/view/validProds.php?prod=SVR&node=KBUF');

// Strip stuff before <pre> 
$page = substr($page, strpos($page, "<pre>") + 5);
// and after $$
$page = substr($page, 0, strpos($page, "$$"));

// At this point, $page is equal to everything inbetween the <pre> tags

// Find the date like: 1028 AM EST MON DEC 15 2008
$success = preg_match('([0-9]{3,4} [A-Z]{2} [A-Z]{3,4} [A-Z]{3} [A-Z]{3} [0-9]{1,2} [0-9]{4})', $page, $matches);

if($success){
$page = substr($page, strpos($page, $matches[0]) + strlen($matches[0]));	
}

echo $page;

?>

 

For me this gave:

 

THE NATIONAL WEATHER SERVICE IN BUFFALO HAS ISSUED A

 

* SEVERE THUNDERSTORM WARNING FOR...

  NORTHWESTERN CATTARAUGUS COUNTY IN WESTERN NEW YORK

  NORTHERN CHAUTAUQUA COUNTY IN WESTERN NEW YORK

  ERIE COUNTY IN WESTERN NEW YORK

 

* UNTIL 1130 AM EST

 

* AT 1022 AM EST...NATIONAL WEATHER SERVICE DOPPLER RADAR INDICATED A

  LINE OF SHOWERS AND EMBEDDED THUNDERSTORMS CAPABLE OF PRODUCING

  DAMAGING WINDS IN EXCESS OF 60 MPH.  THESE STORMS WERE LOCATED

  ALONG A LINE EXTENDING FROM 10 MILES EAST OF PORT COLBORNE TO 5

  MILES SOUTHWEST OF FREDONIA...AND MOVING EAST AT 50 MPH.

 

* SEVERE THUNDERSTORMS WILL BE NEAR...

  BUFFALO...KENMORE AND SILVER CREEK BY 1035 AM EST...

  SLOAN...DERBY AND ATHOL SPRINGS BY 1040 AM EST...

  CHEEKTOWAGA...DEPEW AND WILLIAMSVILLE BY 1045 AM EST...

  HARRIS HILL BY 1050 AM EST...

 

THESE ARE DANGEROUS LINE OF SHOWERS. VERY LITTLE LIGHTNING IS

OCCURRING WITH THIS LINE OF SHOWERS. DO NOT WAIT FOR THE SOUND OF

THUNDER TO SEEK SHELTER. PEOPLE OUTSIDE SHOULD MOVE TO A

SHELTER...PREFERABLY INSIDE A STRONG BUILDING BUT AWAY FROM WINDOWS.

 

LAT...LON 4250 7936 4257 7916 4271 7905 4276 7891

      4279 7887 4295 7891 4310 7850 4309 7847

      4252 7847 4229 7966

TIME...MOT...LOC 1526Z 257DEG 39KT 4285 7900 4241 7936

 

If they ever drastically change the page though, it would need to be modified.

Link to comment
Share on other sites

That is awesome.  Now, is that just the top part of the code or is that the complete file....hmm.  Now, something a little bit more challenging, and I don't even know if this is feasible.  I would like to make it so the warning expires at the time on the page.  Example, if I were to include() this I would like it to expire  at the expiration time and not show up as a warning on the page.

 

i.e....

 

I would like it to show the warning Only if the string "WARNING" was parsed.  Hmm. LOL.

 

EDIT:

I forgot to scroll, I see the whole code now LOL.

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.