4

我有以下代码来解析雅虎天气信息:

$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;

如何修改解析器以适用于负度数?

谢谢你。

4

2 回答 2

4

使破折号可选:

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

您可以从这个演示中看到它打印:

-1
于 2012-12-10T13:28:14.543 回答
1

这与将 codeigniter2.1.4 粘贴到视图文件 .direct 时效果很好

<?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
?>
于 2013-12-26T11:42:09.710 回答