Yahoo Weather Info parsing PHP code unsuitable for negative degrees

I have the following code to parse Yahoo Weather Info:

$xml = simplexml_load_file('http://weather.yahooapis.com/forecastrss?w=868274&u=c'); $weatherInfo = $xml->channel->item->description; $imagePattern = '/src="(.*?)"/i'; preg_match($imagePattern, $weatherInfo, $matches); $imageSrc = $matches[1]; $degreesPattern = '/.*?, (\d+) C/i'; preg_match($degreesPattern, $weatherInfo, $matches); $degrees = $matches[1]; echo $degrees; 

How can I change the parser to work in negative degrees?

Thanks.

+4
source share
2 answers

Make a dash an option:

 $degreesPattern = '/.*?, (-?\d+) C/i'; ^^ 

You can see from this demo that he is printing:

 -1 
+4
source

this works well with codeigniter2.1.4 in a .direct file

 <?php // weather City qalat-dizah $xml = simplexml_load_file('http://weather.yahooapis.com/forecastrss?w=1977965&u=c'); $weatherInfo = $xml->channel->item->description; $imagePattern = '/src="(.*?)"/i'; preg_match($imagePattern, $weatherInfo, $matches); $imageSrc = $matches[1]; echo img($imageSrc) .'<br/>'; $degreesPattern = '/.*?, (-?\d+) C/i'; preg_match($degreesPattern, $weatherInfo, $matches); $degrees = $matches[1]; echo $degrees .'<br/>'; // end ?> 
+1
source

All Articles