Copy codeThe Code is as follows:
<? Php
// The country determines if $ REQUEST_URI contains html
If (! Strpos ($ _ SERVER ["REQUEST_URI"], ". html "))
{
$ Page = "http://qq.ip138.com/weather ";
$ Html = file_get_contents ($ page, 'R ');
$ Pattern = "/<B> online query of weather trend forecasts for major cities, counties, and the next five days <\/B> (.*?) <Center style = \ "padding \: 3px \">/si ";
// Html between regular expression matching
Preg_match ($ pattern, $ html, $ pg );
Echo "";
// Replace the remote address with the local address
$ P = preg_replace ('// weather \/(\ w +) \/index.htm/', 'tq. php/$1.html ', $ pg [1]);
Echo $ p;
}
// Save. Check whether the condition is $ REQUEST_URI?
Else if (! Strpos ($ _ SERVER ["REQUEST_URI"], "? ")){
// Use the separation method recommended by yoyo to obtain data. Here, the province name is obtained.
$ Province = explode ("/", $ _ SERVER ["REQUEST_URI"]);
$ Province = explode (".", $ province [count ($ province)-1]);
$ Province = $ province [0];
// The comment is the regular expression written by myself. It is not easy to write, but the effect is equivalent to the above.
// Preg_match ('/[^ \/] + [\. (html)] $/', $ _ SERVER ["REQUEST_URI"], $ pro );
// $ Province = preg_replace ('/\. html/', '', $ pro [0]);
$ Page = "http://qq.ip138.com/weather/". $ province. "/index.htm ";
// Try to open the page before obtaining html data to prevent error caused by malicious address input
If (! @ Fopen ($ page, "r ")){
Die ("sorry, this address does not exist! <A href = javascript: history. back (1)> click here to return </a> ");
Exit (0 );
}
$ Html = file_get_contents ($ page, 'R ');
$ Pattern = "/five-day weather trend forecast <\/B> (.*?) Enter city/si ";
Preg_match ($ pattern, $ html, $ pg );
Echo "";
// Obtain the province and city for regular expression replacement.
$ P = preg_replace ('// weather \/(\ w +) \/(\ ww.20..htm/', '%2.html? Pro = $ 1', $ pg [1]);
Echo $ p;
}
Else {
// City, pass the province through get
$ Pro = $ _ REQUEST ['pro'];
$ City = explode ("/", $ _ SERVER ["REQUEST_URI"]);
$ City = explode (".", $ city [count ($ city)-1]);
$ City = $ city [0];
// Preg_match ('/[^ \/] + [\. (html)] + [\?] /', $ _ SERVER ["REQUEST_URI"], $ cit );
// $ City = preg_replace ('/\. html \? /', '', $ Cit [0]);
$ Page = "http://qq.ip138.com/weather/". $ pro. "/". $ city. ". htm ";
If (! @ Fopen ($ page, "r ")){
Die ("sorry, this address does not exist! <A href = javascript: history. back (1)> click here to return </a> ");
Exit (0 );
}
$ Html = file_get_contents ($ page, 'R ');
$ Pattern = "/five-day weather trend forecast <\/B> (.*?) Enter city/si ";
Preg_match ($ pattern, $ html, $ pg );
Echo "";
// Obtain the real image address
$ P = preg_replace ('/\/image \ //', 'HTTP: // qq.ip138.com/image/', $ pg [1]);
Echo $ p;
}
?>