Jump to content

extract weather data. should be simple.


muppet77

Recommended Posts

<?php

$xml_feed="http://feeds.bbc.co.uk/weather/forecast/2818/Next3DaysRSS.xml";
if($xml = simplexml_load_file("$xml_feed"));
foreach ($xml->channel->item as $value) {
$title=$value->title;
$title=utf8_decode($title);
$title=str_replace(",","<br />",$title);
echo "<p>$title</p>";
}

?>

ok i now have this as code:

 

<?php 

// set name of XML file 
$file = "http://feeds.bbc.co.uk/weather/forecast/2818/Next3DaysRSS.xml"; 

// load file 
$xml = simplexml_load_file($file) or die ("Unable to load XML file!"); 


foreach ($xml->channel->item as $value) {
$title=$value->title;
$title=utf8_decode($title);
$title=str_replace(",","<br />",$title);
$length = strpos($title, "Min Temp:") - strpos($title, "Max Temp:") - 26; 
echo substr($title,strpos($title, "Max Temp:")+10,$length);
}
?> 

 

which gives

 

-3-11

 

how can i name them 3 separate variables?

 

i am soo close..

Here is something for you to work with.

<?php
$file = "http://feeds.bbc.co.uk/weather/forecast/2818/Next3DaysRSS.xml"; 
$xml = simplexml_load_file($file);


$temps_array = $xml->xpath('channel/item/description');
$temp = explode(',',$temps_array[0]);

echo $temp[0] . '<br />' . $temp[1];
?>

thanks, that now produces:

 

Max Temp: -1°C (30°F)

Min Temp: -8°C (18°F)

 

i really just want the -1 as a variable and also the -8.

 

...and then the next day aswell..?

 

i am a real beginner to all this. they are arrays right?

got there. works a treat. i only wanted tomorrow's max and min.

 

<?php
$file = "http://feeds.bbc.co.uk/weather/forecast/2818/Next3DaysRSS.xml"; 
$xml = simplexml_load_file($file);
$temps_array = $xml->xpath('channel/item/description');
$temp2 = explode(',',$temps_array[1]);
$max = substr(trim($temp2[0]),10,strlen(trim($temp2[0]))-21);
$min = substr(trim($temp2[1]),10,strlen(trim($temp2[1]))-21);
echo $max;
echo $min;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.