首页 > 编程知识 正文

php抓取div数据(php获取div指定内容)

时间:2023-12-18 17:21:45 阅读:317084 作者:MBJN

本文目录一览:

PHP html正则提取div数据

正则提取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" 

       tbodytr  

  td width="100"pspan潮时 (Hrs)/span/p/td  

     td width="100"p00:58/p/td

     td width="100"p05:20/p/td

     td width="100"p13:28/p/td

     td width="100"p21:15/p/td

          /tr  

       tr  

  tdpspan潮高 (cm)/span/p/td  

     td width="100"p161/p/td  

     td width="100"p75/p/td  

     td width="100"p288/p/td  

     td width="100"p127/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';  

}

?

PHP正则提取 DIV CLASS 数据

用PHP正则表达式匹配,就可以提取class里面的数据,并将no去除,完整的PHP程序如下

?php

 $str='div class="lot-nums"span class="no7"/spanspan class="no8"/spanspan class="no5"/spanspan class="no9"/spanspan class="no2"/spanspan class="no4"/spanspan class="no6"/spanspan class="no0"/spanspan class="no3"/span span class="no1"/span/div'; 

 $regex='/span class="no(d+)"/i'; 

 preg_match_all($regex,$str,$result); 

 print_r($result[1]); 

?

运行结果

如何用PHP 取DIV标签内容

如果你要和之间的所有源码,用preg_match就可以,不用preg_match_all,如果你要里面的所有的标签中的内容,可以用preg_match_all//提取所有代码$pattern='/(.+?)/is';preg_match($pattern,$string,$match);//$match[0]即为和之间的所有源码echo$match[0];//然后再提取之间的内容$pattern='/(.+?)/is';preg_match_all($pattern,$match[0],$results);$new_arr=array_unique($results[0]);foreach($new_arras$kkk){echo$kkk;}

php正则 提取DIV里面的数据

你这里内容是固定的日期时间格式,直接写个相应的格式就行了啊,其他不用管

(d{4}-d{2}-d{2})

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。