Php Regular Expression matches the div with a class in html and selects the content method.

Source: Internet
Author: User
Tags php regular expression

Php Regular Expression matches the div with a class in html and selects the content method.

This article mainly introduces the php Regular Expression matching html div with class and selects the content method, which involves the use of curl and regular expression matching techniques. It has some reference value, for more information, see

 

 

This example describes how to use php regular expression to match the div with class in html and select the content. Share it with you for your reference. The specific analysis is as follows:

First look at the html code:

 

The Code is as follows:

<Div class = "chartInfo">
<Div class = "line"> </div>
<Div class = "tideTable">
<Strong> tide table </strong> data is for reference only
<Table width = "500" border = "0" cellspacing = "0" cellpadding = "0">
<Tbody> <tr>
<Td width = "100"> <p> <span> Hrs </span> </p> </td>
<Td width = "100"> <p> </td>
<Td width = "100"> <p> </td>
<Td width = "100"> <p> </td>
<Td width = "100"> <p> </td>
</Tr>
<Tr>
<Td> <p> <span> high tide (cm) </span> </p> </td>
<Td width = "100"> <p> 161 </p> </td>
<Td width = "100" type = "codeph" text = "codeph"> <p> 75 </p type = "codeph" text = "/codeph"> </td type = "codeph" text = "/codeph">
<Td width = "100"> <p> 288 </p> </td>
<Td width = "100"> <p> 127 </p> </td>
</Tr>
</Tbody> </table>
<H2> time zone:-1000 (East 10) Tidal high reference: 174 at average sea level </Div>
<Div class = "chart">
</Div>
</Div>

 

This is a part of the source program. In order to make it easy to understand, a majority of content is removed, and only the content in the <div class = "tideTable"> div block is used.

On the homepage, use file_get_content or curl to obtain the content. I use curl.

The Code is as follows:

$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
$ Return = curl_exec ($ ch );
Curl_close ($ ch );

$ Regex4 = "/<div class = \" tideTable \ ". *?>. *? <\/Div>/ism ";
If (preg_match_all ($ regex4, $ return, $ matches )){
Print_r ($ matches );
} Else {
Echo '0 ';
}

 

In this way, you can understand PHP code and print the results.

I hope this article will help you with php programming.

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.