The PHP code used to analyze Yahoo Weather Info failed due to negativeness.

I have the following code to parse Yahoo weather information:

$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 to modify the parser to use negative degrees?

Thank you.

Make the dash optional:

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

You can see from this demo To it prints:

-1

I have the following code to parse Yahoo weather information:

$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 to modify the parser to use negative degrees?

Thank you.

Make the dash optional:

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

You can see it prints from this demo:

< /p>

-1

Leave a Comment

Your email address will not be published.