php代码
<?php /* *特别注意,第一天没有最高气温数据,第八天没有最低气温数据 *注意对数字进行过滤时不要忘记对负号进行判断 *对风力过滤时要考虑到3-5级这种格式 */ class weatherfetch{ private $f; function getNum($string) { $tmpstr = ''; $strlen = strlen($string); for($i=0; $i<$strlen; $i++) { $str=substr($string, $i, 1); $str1=trim($str); if(is_numeric($str1)){ $tmpstr.=$str1+0; } if($str1=="-"&&is_numeric(substr($string, $i-1, 1))){ $tmpstr.= $str1; } } return $tmpstr; } function __construct(){ $this->f= new SaeFetchurl(); } function getChineseNum($string){ $tmpstr = ''; $arr = array(1,2,3,4,5,6,7,8,9,0); $strlen = strlen($string); for($i=0; $i<$strlen; $i++) { $str=substr($string, $i, 1); $str1=trim($str); if( ord($str)>0xA0 ){ $tmpstr.= substr($string, $i, 3); $i = $i+2; } if(is_numeric($str1)){ $tmpstr.= $str1; } if($str1=="-"&&is_numeric(substr($string, $i-1, 1))&&is_numeric(substr($string, $i+1, 1))){ $tmpstr.= $str1; } } return $tmpstr; } function getChinese($string,$encode="GBK") { switch($encode){ case "GBK" :$codelength=2;break; case "GB2312" :$codelength=3;break; case "UTF-8" :$codelength=3;break; case "UTF-16" :$codelength=4;break; } $tmpstr = ''; $arr = array(1,2,3,4,5,6,7,8,9,0); $strlen = strlen($string); for($i=0; $i<$strlen; $i++) { $str=substr($string, $i, 1); $str1=trim($str); if( ord($str)>0xA0 ){ $tmpstr.= substr($string, $i, $codelength); $i = $i+$codelength-1; } } return $tmpstr; } function get($cityid){ $url="http://www.weather.com.cn/weather/".$cityid.".shtml"; $data=$this->f->fetch($url); $sun=explode('<div class="weatherTopright">',$data); $sun=explode("<dl>",$sun[1]); $sun=explode("</dl>",$sun[1]); $sun=explode("</strong>",$sun[0]); $sunrise=strlen($sun[0]); $sunrise=substr($sun[0],$sunrise-5);//日出时间 $sunset=strlen($sun[1]); $sunset=substr($sun[1],$sunset-5);//日落时间 $sunhour=substr($sunset,0,2)-substr($sunrise,0,2); $sunminute=$sunhour*60+substr($sunset,-2)-substr($sunrise,-2);//日照时间 $yubao=explode('class="yuBaoTable"',$data); $num=count($yubao); $tl=array(); $th=array(); $fx=array(); $fl=array(); $weather=array(); //第一天 $tr=explode("</tr>",$yubao[1]); $td=explode("</td>",$tr[0]); $weather[]=$this->getChinese($td[3],"UTF-8");//晚上天气 $fx[]=$this->getChinese($td[5],"UTF-8");//晚上风向 $fl[]=substr($this->getChineseNum($td[6],"UTF-8"),5);//晚上风力 $tltemp=explode("<strong>",$td[4]);//最低气温 $tl[]=$this->getNum($tltemp[1]); //从第二天到第七天 for($i=2;$i<$num-1;$i++){ $tr=explode("</tr>",$yubao[$i]); $td=explode("</td>",$tr[0]); $weather[]=$this->getChinese($td[3],"UTF-8");//白天天气 $fx[]=$this->getChinese($td[5],"UTF-8");//白天风向 $fltemp=substr($this->getChineseNum($td[6],"UTF-8"),5); $fl[]=$fltemp;//白天风力 $thtemp=explode("<strong>",$td[4]); $th[]=$this->getNum($thtemp[1]);//最高气温 $td=explode("</td>",$tr[1]); $tltemp=explode("<strong>",$td[3]); $tl[]=$this->getNum($tltemp[1]);//最低气温 } //第八天 $tr=explode("</tr>",$yubao[$num-1]); $td=explode("</td>",$tr[0]); $weather[]=$this->getChinese($td[3],"UTF-8");//白天天气 $fx[]=$this->getChinese($td[5],"UTF-8");//白天风向 $fl[]=substr($this->getChineseNum($td[6],"UTF-8"),5);//白天风力 $thtemp=explode("<strong>",$td[4]); $th[]=$this->getNum($thtemp[1]);//最高气温 if(count($weather)==8){ return array("weather"=>$weather,"tl"=>$tl,"th"=>$th,"fx"=>$fx,"fl"=>$fl,"sunset"=>$sunset,"sunrise"=>$sunrise,"sunminute"=>$sunminute); }else{ return 1; } } function getday1($cityid){ $url="http://www.weather.com.cn/weather/".$cityid.".shtml"; $data=$this->f->fetch($url); $yubao=explode('class="yuBaoTable"',$data); $tr=explode("</tr>",$yubao[1]); $td=explode("</td>",$tr[0]); $thtemp=explode("<strong>",$td[4]); return $this->getNum($thtemp[1]); } }
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사
어 ass 신 크리드 그림자 : 조개 수수께끼 솔루션
3 몇 주 전ByDDD
Windows 11 KB5054979의 새로운 기능 및 업데이트 문제를 해결하는 방법
2 몇 주 전ByDDD
Atomfall에서 크레인 제어 키 카드를 찾을 수 있습니다
3 몇 주 전ByDDD
<s> : 데드 레일 - 모든 도전을 완료하는 방법
4 몇 주 전ByDDD
Atomfall Guide : 항목 위치, 퀘스트 가이드 및 팁
1 몇 달 전ByDDD

뜨거운 도구

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

맨티스BT
Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

mPDF
mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.
