Hello there all. I’m just learning PHP and I wanted to start by making a script which would read an RSS feed, strip out what I do not want to display, and output only one number (or an average of them all). In my case, I am reading the RSS from a government website which tracks fuel prices.
I’m able to pull the RSS feed, display the data and so forth. My problem is, the array contains everything in one element (?). So not only are price being displayed, but other information which (for me) is unnecessary. I only need one price, or an average of them all.
I have tried limiting the amount of data displayed, and various methods of stripping out the things like state names, etc. However, the method I am using displays every number and (since I need the decimal points), displays EVERY decimal point in the text, not just those in the fuel prices.
Now please don’t be too harsh. I’ve been trying for a while with codeviper and then my local xampp install to do this. My var_dump displays close to what I want, but you’ll see the problem occurs with my filter. And also I cannot select an individual gasoline price because of the way the RSS feed is formatted. That data is under one block (description), so I have to pull it all (I think).
Here is what I have so far (the commented out code was an earlier attempt to limit the amount of data displayed from the stream to perhaps make it easier to filter or search for and match a pattern).
[php]<?php
$rss = new DOMDocument();
$rss->load(‘http://www.eia.gov/petroleum/gasdiesel/includes/gas_diesel_rss.xml’);
$feed = array();
foreach ($rss->getElementsByTagName(‘item’) as $node) {
$item = array (
‘desc’ => $node->getElementsByTagName(‘description’)->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 1;
for($x=0;$x<$limit;$x++) {
$description = $feed[$x]['desc'];
// only to limit the number of items displayed $description = substr($description, 0, 160);
echo '<p>'.$description.'</p>';
}
$arr = $item;
foreach($arr as &$item) {
$item = preg_replace(’/[^0-9.]*/’, ‘’, $item);
}
var_dump($arr);
?>[/php]
Any pointers would be appreciated. I’ve been searching the PHP manual and trying various filters. Ideally, I want to be able to display only one number (the US average would be fine… or simply to be able to find all the numbers, average them, set it as a variable and display that). I’m a bit stuck ???