2017-02-14 75 views
1

有人可以幫我鑑定在這裏的錯誤:與文件PHP錯誤獲取內容

<?php 
$city=$_GET['city']; 
$city=str_replace(" ", "", $city); 
[email protected]_get_contents("http://www.weather-forecast.com/locations/'.$city'/forecasts/latest"); 
preg_match('/<span class="phrase">(.*?)</s', $contents, $matches); 
echo @$matches[1]; 
?> 

出於某種原因的代碼是給我的錯誤,但是,如果我刪除了城市和特定城市進行更換(倫敦,巴黎,開羅)的代碼工作,但是當我把它放在城市..代碼不起作用。

有人可以幫助我確定錯誤或如何工作。

+0

改變這個'「http://www.weather-forecast.com/locations/'.$city'/forecasts/latest」''到「http://www.weather-forecast.com/locations/$city/forecasts/latest」'。然後嘗試 –

+0

重新格式化您的問題(不需要粗體)。在執行$ contents行之前,$ city的價值是多少? –

+0

字符串連接出現錯誤。 –

回答

0

首先從您的所有代碼行中刪除@(不要從錯誤中逃脫,嘗試查找並解決它們)。

第二個變化$contents線象下面這樣: -

$contents= file_get_contents("http://www.weather-forecast.com/locations/$city/forecasts/latest"); 

或者

$contents= file_get_contents("http://www.weather-forecast.com/locations/'".$city."'/forecasts/latest"); 

注: - 字符串連接問題是有在你的代碼

+0

謝謝..現在它工作...我用你的$城市和工作。 – Farida

+0

@法裏達很高興幫助你。 –

0
<?php 
$city=$_GET['city']; 
$city=str_replace(" ", "", $city); 
$contents=file_get_contents('http://www.weather-forecast.com/locations/'.$city.'/forecasts/latest'); 
preg_match('/<span class="phrase">(.*?)</s', $contents, $matches); 
echo $matches[1]; 
?> 
+3

請不要只是郵編,而是說明關鍵的變化。 – mkl

0

能否請您檢查$城市變量的分析是這樣

$contents = @file_get_contents("http://www.weather-forecast.com/locations/".$city."/forecasts/latest");