PHP html正则提取div数据

PHP html正则提取div数据,第1张

正则提取div数据主要是使用PHP的file_get_content()函数。

具体示例:

HTML代码:

<div class="chartInfo">  

 <div class="line"></div>

 <div class="tideTable">  

     <strong>潮汐表</strong>数据仅供参考

     <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">  

 </div>  

</div>

首页先用file_get_content或curl获取内容部分

PHP的正则处理程序:

<?php

$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'  

}

?>

第一种,使用smarty模板引擎

php文件:

$smarty->assign('data','hello world')

$smarty->display('index.html')

index.html文件:

<div>{$data}</div>

输出hello world

第二种,使用PHP变量直接输出

php文件:

$data = 'hello world'

require 'index.html'

index.html:文件:

<div><?php echo $data?></div>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10865314.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存