cloudy

Author Topic: Great New Weather Script - Need PHP Help (long)  (Read 3996 times)

0 Members and 1 Guest are viewing this topic.

Offline 1080iAddict

  • Posts: 45
Great New Weather Script - Need PHP Help (long)
« on: June 07, 2006, 12:14:37 PM »
Guys:

I found a great new Weather Script on the net.  It's actually a Universal PHP RSS Reader that I configured to read the NOAA Alerts RSS Feed, but it can be configured for any weather related RSS feed (or non-weather related, of course).

There are two versions of the Script and you can try both at the same time.  As I configured it (see below), the first Script, ("pscroller1"), will vertically scroll the RSS "Items" for a given NWS Zone, in my case, MDZ006.  You can easily change the zone by simply copying the URL for the RSS for your zone by finding the URL at http://www.weather.gov/alerts/ and then configuring the PHP setup pages that come with the Script.  So for this script, I get the headlines "Severe Thunder Storm Warning", 'Special Weather Statement", etc. for my zone, and that's it (no Description), but they scroll very cleanly and "snappy" one at a time in the display box.  It's very neat.

The second Script, ("pscroller2"), will vertically scroll the RSS "Item" and "Description" for a given NWS Zone.  So for this script, I get the headlines and the descriptions / text for the "Severe Thunder Storm Warning", 'Special Weather Statement", etc.  The 'rub' is that they scroll one at a time - and PAUSE for each one for a configurable time - in the display box.  It's VERY neat.

Here is the script: 

http://www.dynamicdrive.com/dynamicindex17/rsspausescroller/index.htm

Here are the instructions:

1.  Download the zip file on that web page.

2.  Follow the instructions step by step - carefully - on that web page.  Be sure to put ALL the zipped files and sub-directories in the same directory in your webserver.  In other words, There is one HTM page ("demo.htm") and one file ("rsspausescroller.js") that you place in your webserver directory, but you also place the subdirectory ("lastrss"), just like it is, in the same webserver directory.  That subfolder has two PHP pages and an additional cache subfolder which will copy over when you place the files in your webserver.

3.  Load up the demo.htm and be sure it works properly.  If it does not, check the directions on the web page above again.  It worked for me the first time.

4.  OK, so now you see how it works and you need to configure it for NOAA RSS.  Go here:   http://www.weather.gov/alerts/ and then in the THIRD column where it says "RSS 'By County/Zone'" you click the orange XML button for your state.  Next, click on your county.  When the RSS feed displays in your browser, highlight and copy the URL link.

5.  Back at your webserver, edit the PHP file "scrollerbridge.php" in the "lastrss" subdirectory. 

6.  You will see some code that looks like this:

Code: [Select]

// Define your list of RSS URLs- "RSS_id"=> "URL to RSS feed"
$rsslist=array(
"cnn" => "http://rss.cnn.com/rss/cnn_topstories.rss",
"bbc" => "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml",
"news.com" => "http://news.com.com/2547-1_3-0-5.xml",
"slashdot" => "http://rss.slashdot.org/Slashdot/slashdot",
"dynamicdrive" => "http://www.dynamicdrive.com/export.php?type=new",
"WJZ" => "http://wjz.com/localbriefs/resources_genericrss.xml",
"NOAA" => "http://www.weather.gov/alerts/wwarssget.php?zone=WIZ046",
);


Notice the last line I added that starts "NOAA".  You need to add this line, and insert the URL you copied as a replacement to what I inserted.  Remember to add the comma after the last quote.  Now close that page.  You're done.  You do not need to edit the other PHP file called "lastRSS.php".

7.  Now go to the "demo.htm" page that come with the script and edit that page.  You will find this code at the end of that page:

Code: [Select]

new rsspausescroller("NOAA", "pscroller1", "rssclass", 3000, "_new")

document.write("<br />") //add a divider for demo

new rsspausescroller("NOAA", "pscroller2", "rssclass", 3000, "", "date+description")

   
See how I adjusted the RSS feed to "NOAA" instead of whatever they had there (I think it was cnn)?  Well, change this to NOAA.  This now pulls the feed from what you just set up in "scrollerbridge.php".
 
8. Open up "demo.htm" in your browser.  If there are no alerts for your area, I would suggest going back to  http://www.weather.gov/view/nationalwarnings.php?map=on and looking at our fine country, and work backwards to find some juicy warnings and watches and select that zone temporarily.  I have gotten fairly good at this because warnings and watches change and can suddenly drop when the warning is over.  Go back to "scrollerbridge.php" and just substitute the zone number (the rest of the URL stays the same) so you can see the script in action.

9.  COOL, eh?  But wait.... there's a problem.  That is why I took the time to post this.   :D

-------------

If you look at the Script (that shows the RSS "Description"), i.e., the second script, you will notice that it displays like this:



PROVIDENCE COUNTY IN RHODE ISLAND<br> SOUTHERN NORFOLK COUNTY IN EASTERN MASSACHUSETTS<br> THIS INCLUDES THE CITY OF FRANKLIN<br><br>* UNTIL 145 PM EDT<br><br>* AT 755 AM EDT RADAR INDICATES HEAVY RAIN WILL CONTINUE THROUGH AT<br> LEAST NOON BEFORE DIMINISHING BETWEEN 1 AND 4 PM. RAINFALL<br> OVERNIGHT ENDING AT 730 AM HAS AVERAGED 1 TO 2 INCHES ACROSS MUCH<br> OF PROVIDENCE COUNTY RHODE ISLAND AND SOUTHERN NORFOLK COUNTY.<br><br>* HEAVIEST RAIN SO FAR HAS OCCURRED FROM FOXBOROUGH TO PLAINVILLE<br> MASSACHUSETTS TO CUMBERLAND...LINCOLN...SMITHFIELD...WOONSOCKET<br> AND NORTH PROVIDENCE.<br><br>* THE HEAVY RAIN WILL CAUSE WIDESPREAD POOR DRAINAGE AND BASEMENT<br> FLOODING BY LATE THIS MORNING WITH SEVERAL STREAMS EXPECTED TO<br> RIDE OUT OF THEIR BANKS DURING MIDDAY.<br><br>* THE BLACKSTONE AND NORTH BRANCH OF THE PAWTUXET RIVERS WILL BE<br> RISING RAPIDLY DURING MIDDAY AND THIS AFTERNOON...THREATENING TO<br> FLOOD.<br><br>* ROAD CLOSURES MAY OCCUR BY LATE MORNING. IF ROADS BECOME<br> IMPASSABLE...DONT DRIVE AROUND BARRICADES. IT COULD BECOME<br> LIFE THREATENING.<br><br>* STORM TOTAL RAINFALL BY NOON IN PORTIONS OF NORFOLK AND<br> PROVIDENCE COUNTIES WILL PROBABLY EXCEED 4 INCHES...WITH 6 INCH<br> TOTALS POSSIBLE.<br><br>A FLOOD WARNING MEANS THAT FLOODING IS IMMINENT OR HAS BEEN REPORTED.<br>STREAM RISES WILL BE SLOW AND FLASH FLOODING IS NOT EXPECTED.<br>HOWEVER...ALL INTERESTED PARTIES SHOULD TAKE NECESSARY PRECAUTIONS<br>IMMEDIATELY.<br><br>LAT...LON 4177 7178 4176 7145 4197 7141 4210 7107<br> 4218 7132 4199 7149 4198 7178<br><br>$$<br><br><br>DRAG<br><a href=http://www.nws.noaa.gov/er/box/>Issuing Weather Forecast Office Homepage</a>Flood Watch - Southeast Providence (Rhode Island)
Dec 31, 1969 7:00:00 PM




Notice how this script is not 'reading' the line breaks in the RSS feed but just displaying the html instead?  I want to make the script display the results better, i.e., literally enforce the line breaks or instead remove that html and have it flow better.  Ideally, I want it to display just like it does with the "<pre>" html command....or the way it does when you read the page over IWIN, etc. so it looks 'normal' as I am used too the way I normally read it on the net.

So I AM NOT a PHP guy.  I only tinker.  After hours of bs-ing around to tinker with the script, I managed to get some progress - but then hit a dead end.  At the end of the "scrollerbridge.php" page, I found this code:

Code: [Select]

function slas**t($what){ //Encode text for storing in JavaScript array
$newstring=str_replace('&apos;', '\'', $what); //replace those half valid apostrophe entities with actual apostrophes
return rawurlencode($newstring);


I replaced that middle line (sloppy...but as a test and then ill add it 'in addition to' the middle line) with the following code:

Code: [Select]

$newstring=preg_replace("<br>", " ", $what); //replace those half valid apostrophe entities with actual apostrophes


Now, it displays like this:



PROVIDENCE COUNTY IN RHODE ISLAND<> SOUTHERN NORFOLK COUNTY IN EASTERN MASSACHUSETTS<> THIS INCLUDES THE CITY OF FRANKLIN<><>* UNTIL 145 PM EDT<><>* AT 755 AM EDT RADAR INDICATES HEAVY RAIN WILL CONTINUE THROUGH AT<> LEAST NOON BEFORE DIMINISHING BETWEEN 1 AND 4 PM. RAINFALL<> OVERNIGHT ENDING AT 730 AM HAS AVERAGED 1 TO 2 INCHES ACROSS MUCH<> OF PROVIDENCE COUNTY RHODE ISLAND AND SOUTHERN NORFOLK COUNTY.<><>* HEAVIEST RAIN SO FAR HAS OCCURRED FROM FOXBOROUGH TO PLAINVILLE<> MASSACHUSETTS TO CUMBERLAND...LINCOLN...SMITHFIELD...WOONSOCKET<> AND NORTH PROVIDENCE.<><>* THE HEAVY RAIN WILL CAUSE WIDESPREAD POOR DRAINAGE AND BASEMENT<> FLOODING BY LATE THIS MORNING WITH SEVERAL STREAMS EXPECTED TO<> RIDE OUT OF THEIR BANKS DURING MIDDAY.<><>* THE BLACKSTONE AND NORTH BRANCH OF THE PAWTUXET RIVERS WILL BE<> RISING RAPIDLY DURING MIDDAY AND THIS AFTERNOON...THREATENING TO<> FLOOD.<><>* ROAD CLOSURES MAY OCCUR BY LATE MORNING. IF ROADS BECOME<> IMPASSABLE...DONT DRIVE AROUND BARRICADES. IT COULD BECOME<> LIFE THREATENING.<><>* STORM TOTAL RAINFALL BY NOON IN PORTIONS OF NORFOLK AND<> PROVIDENCE COUNTIES WILL PROBABLY EXCEED 4 INCHES




See?  It removes the "<BR>" but leaves the "<" and the ">".

If instead I use this code:

Code: [Select]

$newstring=preg_replace("<br>", "<p>", $what); //replace those half valid apostrophe entities with actual apostrophes


It will display like this:



PROVIDENCE COUNTY IN RHODE ISLAND<

> SOUTHERN NORFOLK COUNTY IN EASTERN MASSACHUSETTS<

> THIS INCLUDES THE CITY OF FRANKLIN<

><

>* UNTIL 145 PM EDT<

><

>* AT 755 AM EDT RADAR INDICATES HEAVY RAIN WILL CONTINUE THROUGH AT<

> LEAST NOON BEFORE DIMINISHING BETWEEN 1 AND 4 PM. RAINFALL<

> OVERNIGHT ENDING AT 730 AM HAS AVERAGED 1 TO 2 INCHES ACROSS MUCH<

> OF PROVIDENCE COUNTY RHODE ISLAND AND SOUTHERN NORFOLK COUNTY.<

><

>* HEAVIEST RAIN SO FAR HAS OCCURRED FROM FOXBOROUGH TO PLAINVILLE<

> MASSACHUSETTS TO CUMBERLAND...LINCOLN...SMITHFIELD...WOONSOCKET<

> AND NORTH PROVIDENCE.<

><

>* THE HEAVY RAIN WILL CAUSE WIDESPREAD POOR DRAINAGE AND BASEMENT



See? 

I must be getting close here but, because I dont know the finer points of PHP, I cant get any further.  I cant get the "<" and ">" to go away.


If anyone wants to tinker with this and suggest a code change, it sure would be appreciated.  I did see on the other PHP page needed for the Script ("lastRSS.php") there is more code and at one point it talks about stripping html and other "bull[edit]" (it actually says it in there) and maybe thats where the tinkering should be....but that part is really confusing to me. 

Good luck should you find yourself as close as me to perfection but not able to make the final step!!!!


Adam

« Last Edit: June 07, 2006, 03:55:50 PM by 1080iAddict »

Offline saratogaWX

  • Posts: 4,672
  • Saratoga, CA, USA 37:16:28N, 122:01:23W - Elev: 374ft.
    • Saratoga Weather
Re: Great New Weather Script - Need PHP Help (long)
« Reply #1 on: June 07, 2006, 03:30:46 PM »
Adam:

The PHP preg_replace uses a Perl regular expression for the first argument, not just a literal string.  A regular expression has a starting and ending delimiter, so you might try:
Code: [Select]
$newstring=preg_replace('|<br>|', " ", $what); where the | acts as a regex delimiter.

What may have happened deep in the code is an escaping of the description string such that all the real HTML markup is replaced by the escaped version so that
<br> might have been replaced by &lt;br&gt, which will display as <br> on a browser page, but won't be acted on by the browser to do a 'break'.

If that's the case, you can try:
Code: [Select]
$newstring=preg_replace('|&lt;br&gt;|s', "<br>", $what); which would reconstitute the 'real' HTML for <br>

I'm only a dabbler in PHP too.. your mileage may vary, void where prohibited by law, etc.  :D

Best regards,
Ken
Ken True
Saratoga Weather
CWOP: CW1792
WeatherUnderground: KCASARAT1
Free weather website PHP scripts and WD website AJAX templates

Offline 1080iAddict

  • Posts: 45
Re: Great New Weather Script - Need PHP Help (long)
« Reply #2 on: June 07, 2006, 03:42:21 PM »
Thanks Ken.  I already tried your first idea to no avail.  I will try your second idea when I get home tonight.  This is a neat script should you wish to dabble in it.

Offline saratogaWX

  • Posts: 4,672
  • Saratoga, CA, USA 37:16:28N, 122:01:23W - Elev: 374ft.
    • Saratoga Weather
Re: Great New Weather Script - Need PHP Help (long)
« Reply #3 on: June 07, 2006, 04:45:13 PM »
Adam:

I gave it a try, and this code works (does real <br> from the feed):

Code: [Select]
function slas**t($what){ //Encode text for storing in JavaScript array
$newstring=str_replace('&apos;', '\'', $what); //replace those half valid apostrophe entities with actual apostrophes
$newstring=preg_replace('|&lt;br&gt;|s', "<br>", $newstring); // return <br> to HTML form
return rawurlencode($newstring);
}

Ken
Ken True
Saratoga Weather
CWOP: CW1792
WeatherUnderground: KCASARAT1
Free weather website PHP scripts and WD website AJAX templates

Offline 1080iAddict

  • Posts: 45
Re: Great New Weather Script - Need PHP Help (long)
« Reply #4 on: June 07, 2006, 04:53:39 PM »
Dude that's awesome!!!  Its a cool script - right?   After I get this part working, I am going to try and do the custom color stuff we did to your other script so the text and background colors of the dialog box change based on the alerts received.  I like that because you can be across the room and see the status. 

Thanks for the final answer on that part! 

I did note, however, that the "date" function does not work but that can be configured to not be displayed (on the "demo.htm" page) because the date and time within the Description is what really matters. 
« Last Edit: June 07, 2006, 04:55:44 PM by 1080iAddict »

Offline saratogaWX

  • Posts: 4,672
  • Saratoga, CA, USA 37:16:28N, 122:01:23W - Elev: 374ft.
    • Saratoga Weather
Re: Great New Weather Script - Need PHP Help (long)
« Reply #5 on: June 07, 2006, 05:40:19 PM »
Adam:  You're welcome!  Yes, it is a cool script  8)  They do direct parsing of the XML (instead of using the xml_* functions in PHP, and optionally cache the feed for a specified time.

Best regards,
Ken
Ken True
Saratoga Weather
CWOP: CW1792
WeatherUnderground: KCASARAT1
Free weather website PHP scripts and WD website AJAX templates

Offline aifan27

  • Tim
  • Posts: 2,857
  • Pennsylvania
    • Newtown Weather
Re: Great New Weather Script - Need PHP Help (long)
« Reply #6 on: June 07, 2006, 07:47:29 PM »
Adam,

Do you have a screenshot of what it will look like?

Thanks.

--Tim

Offline cayman wx

  • Roger
  • Posts: 550
  • Grand Cayman
    • Weather in Cayman
Re: Great New Weather Script - Need PHP Help (long)
« Reply #7 on: June 07, 2006, 08:20:09 PM »
Ken,

Might this be of help with the NHC issue?

Roger

Offline JaxWeather

  • Bob
  • Global Moderator
  • Posts: 11,374
  • VP 2 w/FARS - WML Station: WD00007
  • Jacksonville, Fl. USA
    • JaxWeather.net
Re: Great New Weather Script - Need PHP Help (long)
« Reply #8 on: June 07, 2006, 08:35:45 PM »
Adam:

I gave it a try, and this code works (does real <br> from the feed):

Code: [Select]
function slas**t($what){ //Encode text for storing in JavaScript array
$newstring=str_replace('&apos;', '\'', $what); //replace those half valid apostrophe entities with actual apostrophes
$newstring=preg_replace('|&lt;br&gt;|s', "<br>", $newstring); // return <br> to HTML form
return rawurlencode($newstring);
}

Ken

That code breaks the scroller i have set up for a test...  maybe a PHP version issue as my Host is still running PHP4 ?

-Bob

Offline 1080iAddict

  • Posts: 45
Re: Great New Weather Script - Need PHP Help (long)
« Reply #9 on: June 07, 2006, 10:08:15 PM »
Bob,

Did you copy / paste that entire 5 lines of code?  Try instead just replacing the 1 line of existing code that starts "$newstring" with the 2 new lines of code above that start "$newstring".  Why?  looking closely I can see that the top line of code above replaced "**" for the letters "hi" because had it not been replaced, it would have spelled a profanity!!!   lol.  look closely at the top line of code.  See those two asterisks?  That should be the letters h and i instead.

I bet you that's it.

Adam   

Offline 1080iAddict

  • Posts: 45
Re: Great New Weather Script - Need PHP Help (long)
« Reply #10 on: June 07, 2006, 10:13:04 PM »
Well Ken, you are brilliant.  Works great now.


Offline saratogaWX

  • Posts: 4,672
  • Saratoga, CA, USA 37:16:28N, 122:01:23W - Elev: 374ft.
    • Saratoga Weather
Re: Great New Weather Script - Need PHP Help (long)
« Reply #11 on: June 07, 2006, 10:39:22 PM »
Adam: Glad it works for you now!  An that is funny.. this SMF board has a 'naughty word' list so substituted ** for it in the code  :lol:

Roger:  I don't think this script would behave any different than the rss-tropical script we've been working on.  The source data is the same (www.nhc.noaa.gov) for the feeds -- this just provides a different way of viewing the RSS feed content.  Getting consistency from the RSS feeds from NHC is the main concern, but until another TD starts up, we won't have data to work with.


Best regards,
Ken
Ken True
Saratoga Weather
CWOP: CW1792
WeatherUnderground: KCASARAT1
Free weather website PHP scripts and WD website AJAX templates

Offline aifan27

  • Tim
  • Posts: 2,857
  • Pennsylvania
    • Newtown Weather
Re: Great New Weather Script - Need PHP Help (long)
« Reply #12 on: June 07, 2006, 10:42:57 PM »
Adam,

Do you have a screenshot of what it will look like?

Thanks.

--Tim

Offline JaxWeather

  • Bob
  • Global Moderator
  • Posts: 11,374
  • VP 2 w/FARS - WML Station: WD00007
  • Jacksonville, Fl. USA
    • JaxWeather.net
Re: Great New Weather Script - Need PHP Help (long)
« Reply #13 on: June 07, 2006, 11:09:53 PM »
Adam,

Good catch and that was it :)  Works as advertised now...

-Bob

Offline JaxWeather

  • Bob
  • Global Moderator
  • Posts: 11,374
  • VP 2 w/FARS - WML Station: WD00007
  • Jacksonville, Fl. USA
    • JaxWeather.net
Re: Great New Weather Script - Need PHP Help (long)
« Reply #14 on: June 07, 2006, 11:24:20 PM »
Here is an AJAX - PHP version of the same thing...
http://www.dynamicdrive.com/dynamicindex17/rsstickerajax/index.htm

-Bob

 

cumulus