Regular expression collection, solving, online, etc.

Source: Internet
Author: User
Regular expression acquisition problem, solving, online and so on target page: http://www.weather.com.cn/weather/101070101.shtml

View the source code of the webpage and get the data of 425 rows, that is, "Today is Wednesday, October 16, 2013" and lunar September 12 rows of "Shenyang weather forecast (released at, 621)" and 641 rows ~ Main data between 765 rows. See the following figure for illustration:


In addition, I wrote a rule and tried to get the data like this: "Shenyang weather forecast (
Released at) "but the input is an empty array! Thank you!


Reply to discussion (solution)

1

$s=file_get_contents('http://www.weather.com.cn/weather/101070101.shtml');preg_match('/\s+(.+)\s+<\!--today5-->/s',$s,$m);echo $m[1];


2
 preg_match('/

\s+\s+(.+?)\s+/s',$s,$m);echo $m[1];



3. I don't know what format of data you want.

I always feel regular expressions are good and high-end

1

$s=file_get_contents('http://www.weather.com.cn/weather/101070101.shtml');preg_match('/\s+(.+)\s+<\!--today5-->/s',$s,$m);echo $m[1];


2
 preg_match('/

\s+\s+(.+?)\s+/s',$s,$m);echo $m[1];



3. I don't know what format of data you want.

Is by date, order to get the value of each item on the line, text on the line, for example: Thursday day high temperature 16 degrees Celsius southwest wind breeze at night sunny low temperature 3 degrees Celsius southwest wind breeze (line feed )... in this way, the data is retrieved.

Source code:

           
 
 
Wednesday 16 Daytime Clear High temperature12 ℃ Northeast wind Breeze
Night Clear Low temperature-2 ℃ Northeast wind Breeze

Only want (get 3 days of data): 16 Thursday day: Sunny 12 ℃ northeast breeze night: Sunny-2 ℃ northeast breeze

I also found a small problem (see). I don't know how to handle it?

I also found a small problem (see). I don't know how to handle it?

Echo preg_replace ('/\ s +/S', '', $ result [1]);

3

Preg_match_all ('/

Oh, wrong. The first one should be echo preg_replace ('/\ s +/S', '', $ result2 [1]);

Question 3
After these two lines are executed, there is a small defect. There are many unnecessary line breaks. can this be done? (View the source code as follows)


Echo preg_replace ('/\ s +/S', '', join ('
', Array_map ('strip _ tags', array_slice ($ m [1], 0, 3 ))));

Echo preg_replace ('/\ s +/S', '', join ('
', Array_map ('strip _ tags', array_slice ($ m [1], 0, 3 ))));

The line feed is removed, but the entire edge is added together.



If not, you can. What should I say? Thank you very much.

Echo preg_replace ('/\ s +/S', '', join ('
', Array_map ('strip _ tags', array_slice ($ m [1], 0, 3 ))));

Today is Thursday, 2013, Shenyang weather forecast (1708-10-: 00 released), Thursday, Sunday, sunny, high temperature, 16 ℃, southwest breeze, breeze, clear at night, low temperature, 3 ℃, southwest breeze
On Friday 18, the daytime is sunny and the high temperature is 18 deg C. The southwest wind is 3-4. the night is cloudy and the low temperature is 6 deg C. The southwest wind is 3-4.
On Saturday 19, a high temperature shower during the day 15 deg C southwest wind 3-4 level nighttime cloudy low temperature 4 deg C southwest wind breeze
Shenyang weather forecast (2013-10-1711: 00 release) on Thursday 17, white sky high temperature 16 deg C southwest breeze Breeze night clear low temperature 3 deg C southwest breeze
On Friday 18, the White Sky is fine, the high temperature is 18 ℃, the southwest wind is 3-4 levels, the night is cloudy, the low temperature is 6 ℃, the southwest wind is 3-4 levels
On Saturday 19, a high temperature shower during the day 15 deg C southwest wind 3-4 level nighttime cloudy low temperature 4 deg C southwest wind breeze
(. +) <\/Table>/isU ', $ s, $ m); echo join ('', Array_map ('strip _ tags', array_slice ($ m [1], 0, 3 ))); Is there a way to change the line feed into a space (the existingReserved )?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.