这不是空白,但脚本错误。这可能是因为你关闭了错误报告。
从这一行:
preg_match('/3 Day Weather Forest Summary:<\/b><span class="phrase">(.*?)</span>',$contents, $matches);
你忘了在</span>
(应该是<\/span>
)逃脱/
; preg_match没有结尾分隔符/
。 (这里有一个输入错误,它应该是'Forecast'not Forest。)
但即使你修复了这些错误,你也不会得到你想要的东西,因为从天气预报中查看html源代码,您在<\/b>
之后跳过<span class="read-more-small"><span class="read-more-content">
。
所以,它应该是这样的:
<?php
$city=$_GET['city'];
$city=str_replace(" ","",$city);
$contents=file_get_contents("http://www.weather-forecast.com/locations/".$city."/forecasts/latest");
preg_match('/3 Day Weather Forecast Summary:<\/b><span class="read-more-small"><span class="read-more-content"> <span class="phrase">(.*?)<\/span>/',$contents, $matches);
echo $matches[1];
?>
或者
你可以使用preg_match_all
获得全部三个天气预报摘要(1 - 3天,4 - 7日,和7 - 10日),将您所有的preg_match
行替换为:
preg_match_all('/<span class="phrase">(.*?)<\/span>/',$contents, $matches);
和回声数据:
$matches[0][0]
为1-3天,
$matches[0][1]
为4-7天,
$matches[0][2]
为7-10天。
你试过调试过吗?什么是城市?你在$内容中得到什么? etc – pvg
空白可能意味着语法错误http://php.net/manual/en/function.error-reporting.php –