Maison > Article > interface Web > php正则匹配html中的带class的div,选取其中的内容_html/css_WEB-ITnose
<div class="chartInfo"> <div class="line"></div> <div class="tideTable"> <strong>潮汐表</strong><span style="font-size:12px;font-weight:lighter;margin-left:-20px;">数据仅供参考</span> <table width="500" border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td width="100"><p><span>潮时 (Hrs)</span></p></td> <td width="100"><p>00:58</p></td> <td width="100"><p>05:20</p></td> <td width="100"><p>13:28</p></td> <td width="100"><p>21:15</p></td> </tr> <tr> <td><p><span>潮高 (cm)</span></p></td> <td width="100"><p>161</p></td> <td width="100"><p>75</p></td> <td width="100"><p>288</p></td> <td width="100"><p>127</p></td> </tr> </tbody> </table> <h2>时区:-1000 (东10区) 潮高基准面:在平均海平面下174CM</h2> </div> <div class="chart"> <!--<img src="http://www.cnss.com.cn/statics/images/cnss/tide/tide.jpg" style="max-width:90%" style="max-width:90%" title="" alt="" /> --> </div> </div>
这是源程序里边的一部分,为了容易看懂,删减了一大部分,只取<span style="font-family: Arial, Helvetica, sans-serif;"><div class="tideTable"> div块中的内容 <pre name="code" class="sycode"><span style="font-family: Arial, Helvetica, sans-serif;">首页先用file_get_content或curl获取内容部分,我用的是curl。</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"></span><span style="white-space:pre"> </span>$ch = curl_init();<span style="white-space:pre"> </span>curl_setopt($ch, CURLOPT_URL, $url);<span style="white-space:pre"> </span>curl_setopt($ch, CURLOPT_POST, 1);<span style="white-space:pre"> </span>curl_setopt( $ch, CURLOPT_HEADER, 0 );<span style="white-space:pre"> </span>curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );<span style="white-space:pre"> </span>curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );<span style="white-space:pre"> </span>$return = curl_exec( $ch );<span style="white-space:pre"> </span>curl_close( $ch );<span style="white-space:pre"> </span><span style="white-space:pre"> </span>$regex4="/<div class='\"tideTable\".*?'>.*?/ism";<span style="white-space:pre"> </span>if(preg_match_all($regex4, $return, $matches)){ <span style="white-space:pre"> </span> print_r($matches);<span style="white-space:pre"> </span>}else{<span style="white-space:pre"> </span>echo '0';<span style="white-space:pre"> </span>} <pre name="code" class="sycode"><span style="font-family:Arial, Helvetica, sans-serif;">这样就可以了,不多解释,懂PHP的看代码,打印看一下效果。</span>