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.