Great New Weather Script - Need PHP Help (long)

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 NWS Public Alerts in XML/CAP v1.1 and ATOM Formats - NOAA's National Weather Service 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: NWS Public Alerts in XML/CAP v1.1 and ATOM Formats - NOAA's National Weather Service 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:



// 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”.

  1. 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:


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

document.write("
") //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”.

  1. Open up “demo.htm” in your browser. If there are no alerts for your area, I would suggest going back to Current Watches, Warnings and Advisories for the United States Issued by the National Weather Service 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.

  2. COOL, eh? But wait… there’s a problem. That is why I took the time to post this. :smiley:


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
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…WITH 6 INCH
    TOTALS POSSIBLE.

A FLOOD WARNING MEANS THAT FLOODING IS IMMINENT OR HAS BEEN REPORTED.
STREAM RISES WILL BE SLOW AND FLASH FLOODING IS NOT EXPECTED.
HOWEVER…ALL INTERESTED PARTIES SHOULD TAKE NECESSARY PRECAUTIONS
IMMEDIATELY.

LAT…LON 4177 7178 4176 7145 4197 7141 4210 7107
4218 7132 4199 7149 4198 7178

$$

DRAG
Issuing Weather Forecast Office HomepageFlood 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 “

” 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:



function slas**t($what){ //Encode text for storing in JavaScript array
$newstring=str_replace(''', '\'', $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:



$newstring=preg_replace("
", " ", $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 "
" but leaves the “<” and the “>”.

If instead I use this code:



$newstring=preg_replace("
", "<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

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:

$newstring=preg_replace('|
|', " ", $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

might have been replaced by <br&gt, which will display as
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:

$newstring=preg_replace('|&lt;br&gt;|s', "
", $what);

which would reconstitute the ‘real’ HTML for

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

Best regards,
Ken

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.

Adam:

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

function slashit($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', "
", $newstring); // return 
 to HTML form
return rawurlencode($newstring);
}

Ken

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.

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

Adam,

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

Thanks.

–Tim

Ken,

Might this be of help with the NHC issue?

Roger

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

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

Well Ken, you are brilliant. Works great now.

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

Adam,

Good catch and that was it :slight_smile: Works as advertised now…

-Bob

Here is an AJAX - PHP version of the same thing…
http://www.dynamicdrive.com/dynamicindex17/rsstickerajax/index.htm

-Bob

Here is the screen shot for those that want to see (the top scroller box is the first script and the bottom scroller box is the second script):

OK. So I played a bit more and added some more code revision to get rid of the NWS hyperlink at the bottom of the RSS Description. This way, it only gives the description and not the hyperlink (which does not work anyway and says a href=http…)


$newstring=str_replace('&apos;', '\'', $what); //replace those half valid apostrophe entities with actual apostrophes
$newstring=preg_replace('|&lt;br&gt;|s', "
", $newstring); // return 
 to HTML form
$newstring=preg_replace("|(a href=http://\S*)|s","", $newstring); // remove NWS hyperlink to clean up display
$newstring=preg_replace("|&lt; Weather Forecast Office Homepage\S*|s","<p>", $newstring); // remove remaining NWS reference stuff to clean up display

I needed to do it by two more lines…there is probably a better way to do it if Ken wants to chime in…but this is how I was able to get it to work.

Adam

Adam,

Do you think you could make the screenshot larger?

Can’t really see it…

Thanks.

–Tim

I know. Sorry Tim. I am challenged here. I tried to upload it and it says file too big. So a photoboard is hosting it and I cant get it bigger. Unfortunantly, I am running the PHP on a intranet so I cant give out a URL. You can see the idea by clicking on the link I first posted to see the script sample page.

Adam

Gotcha…Looks good. 8)

Thanks Adam.

–Tim