天天看点

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 );