xml - Parsing RSS CDATA text outside of tags with PHP -
i know versions of question has been asked before, i've got specific problem in version.
i'm attempting pull text inside rss feed thats imbedded inside cdata, outside xml tags. here's rss file:
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="/rss/ndbcrss.xsl"?> <rss version="2.0" xmlns:georss="http://www.georss.org/georss" xmlns:dc="http://purl.org/dc/elements/1.1/"> <channel> <title>ndbc - station 46042 - monterey - 27nm wnw of monterey, ca observations</title> <description><![cdata[this feed shows recent marine weather observations station 46042.]]></description> <link>http://www.ndbc.noaa.gov/</link> <pubdate>wed, 07 aug 2013 21:06:45 ut</pubdate> <lastbuilddate>wed, 07 aug 2013 21:06:45 ut</lastbuilddate> <ttl>30</ttl> <language>en-us</language> <managingeditor>webmaster.ndbc@noaa.gov</managingeditor> <webmaster>webmaster.ndbc@noaa.gov</webmaster> <image> <url>http://weather.gov/images/xml_logo.gif</url> <title>noaa - national weather service</title> <link>http://www.ndbc.noaa.gov/</link> </image> <item> <pubdate>wed, 07 aug 2013 21:06:45 ut</pubdate> <title>station 46042 - monterey - 27nm wnw of monterey, ca</title> <description><![cdata[ <strong>august 7, 2013 1:50 pm pdt</strong><br /> <strong>location:</strong> 36.785n 122.469w<br /> <strong>wind direction:</strong> sw (220°)<br /> <strong>wind speed:</strong> 1.9 knots<br /> <strong>wind gust:</strong> 1.9 knots<br /> <strong>significant wave height:</strong> 2.3 ft<br /> <strong>dominant wave period:</strong> 14 sec<br /> <strong>average period:</strong> 6.9 sec<br /> <strong>mean wave direction:</strong> sse (160°) <br /> <strong>atmospheric pressure:</strong> 30.11 in (1019.5 mb)<br /> <strong>pressure tendency:</strong> -0.01 in (-0.3 mb)<br /> <strong>air temperature:</strong> 60.8°f (16.0°c)<br /> <strong>water temperature:</strong> 59.9°f (15.5°c)<br /> ]]></description> <link>http://www.ndbc.noaa.gov/station_page.php?station=46042</link> <guid>http://www.ndbc.noaa.gov/station_page.php?station=46042&ts=1375908600</guid> <georss:point>36.785 -122.469</georss:point> </item> </channel> </rss>
i'm trying '2.3 ft', '14 sec', , 'sse (160°)' below lines:
<strong>significant wave height:</strong> 2.3 ft<br /> <strong>dominant wave period:</strong> 14 sec<br /> <strong>mean wave direction:</strong> sse (160°) <br />
i can strip cdata off of , access strong[x] elements, cant figure out how above text outside of tags.
edit
thank carl! using explode/regex worked perfectly. tool added little (but growing) bag.
here's working code used store 3 items:
<?php $url = "http://www.ndbc.noaa.gov/data/latest_obs/46042.rss"; $xml = simplexml_load_file($url); $data = $xml->channel->item->description; foreach (explode("\n", $data) $key=>$line) { preg_match('/(\<strong>.+?\<\/strong>)(.*)?<br/', $line, $matches); if ( ! empty($matches)) { $datadescr[$key] = $matches[1]; $dataval[$key] = $matches[2]; } } $sigwavht = $dataval[5]; $domwavper = $dataval[6]; $meanwavdir = $dataval[8]; echo "$sigwavht, $domwavper, $meanwavdir"; //to test results ?>
if sure data consistent example use regular expression extract data.
for example:
$data = "<strong>significant wave height:</strong> 2.3 ft<br /> <strong>dominant wave period:</strong> 14 sec<br /> <strong>mean wave direction:</strong> sse (160°) <br />"; foreach (explode("\n", $data) $line) { preg_match('/(\<strong>.+?\<\/strong>)(.*)?<br/', $line, $matches); if ( ! empty($matches)) { // part <strong> tags in $matches[1], , // part after in $matches[2] echo "key: {$matches[1]}\tvalue: {$matches[2]}\n"; } }
in looking @ complete feed posted above, you'll want keep in mind first date line not have "data" part after <strong>
content...
Comments
Post a Comment