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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1910 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.