用于解析Yahoo Weather Info的PHP代码因负面度而失败

时间:2012-12-10 13:26:22

标签: php parsing yahoo

我有以下代码来解析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;

如何修改解析器才能使用负度?

谢谢。

2 个答案:

答案 0 :(得分:4)

使破折号可选:

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

您可以从this demo看到它打印出来:

-1

答案 1 :(得分: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
?>