我试图从http://www.weather-forecast.com/locations/Berlin/forecasts/latest
获取天气数据但是preg_match只返回任何内容
<?php
$contents=file_get_contents("http://www.weather-forecast.com/locations/Berlin/forecasts/latest");
preg_match('/3Day Weather Forecast Summary:<\/b><span class="phrase">(.*?)</s', $contents, $matches);
print_r($matches)
?>
&#13;
答案 0 :(得分:2)
不要使用正则表达式解析html,用户使用html解析器,如DOMDocument,
$contents = file_get_contents("http://www.weather-forecast.com/locations/Berlin/forecasts/latest");
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($contents);
$x = new DOMXpath($dom);
foreach($x->query('//span[contains(@class,"phrase")]') as $phase)
{
echo $phase->textContent;
}