preg_match什么都不返回

时间:2016-05-26 12:04:52

标签: php regex

我试图从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;
&#13;
&#13;

1 个答案:

答案 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; 
}