天天看點

PHP抓取天氣預報的代碼

PHP抓取天氣預報的代碼

$strurl = " http://www.cma.gov.cn/tqyb/weatherdetail/54511.html " ;

PHP抓取天氣預報的代碼

$strhtmlarray = file ( $strurl );

PHP抓取天氣預報的代碼

$strlinehtml =   implode ( '' ,   $strhtmlarray ); 

PHP抓取天氣預報的代碼

eregi ( " 3天預報(.*)指數查詢 " ,   $strlinehtml ,   $head );

PHP抓取天氣預報的代碼

$head [ 0 ] = strip_tags ( $head [ 0 ]);

PHP抓取天氣預報的代碼

$head [ 0 ] = str_replace ( "   " , "   " , strip_tags ( $head [ 0 ]));

PHP抓取天氣預報的代碼
PHP抓取天氣預報的代碼

$head [ 0 ] = str_replace ( " 3天預報 " , "" , $head [ 0 ]);

PHP抓取天氣預報的代碼

$head [ 0 ] = str_replace ( " 時間 " , "" , $head [ 0 ]);

PHP抓取天氣預報的代碼

$head [ 0 ] = str_replace ( " 天氣概況 " , "" , $head [ 0 ]);

PHP抓取天氣預報的代碼

$head [ 0 ] = str_replace ( " 氣 溫 " , "" , $head [ 0 ]);

PHP抓取天氣預報的代碼

$head [ 0 ] = str_replace ( " 風向/風力 " , "" , $head [ 0 ]);

PHP抓取天氣預報的代碼

$head [ 0 ] = str_replace ( " 指數查詢 " , "" , $head [ 0 ]);

PHP抓取天氣預報的代碼

$head [ 0 ] = str_replace ( "   " , "" , $head [ 0 ]);

PHP抓取天氣預報的代碼

$result = preg_replace ( " /s+/ " ,   " | " ,   $head [ 0 ]);

PHP抓取天氣預報的代碼

$resultarray = preg_split ( " /[|]/ " , $result );

PHP抓取天氣預報的代碼

// zm_cache::cache_write("weather",$result);

PHP抓取天氣預報的代碼

print_r ( $result );