Home > php教程 > php手册 > body text

php正则匹配html中带class的div并选取其中内容的方法

WBOY
Release: 2016-06-06 20:14:27
Original
1954 people have browsed it

这篇文章主要介绍了php正则匹配html中带class的div并选取其中内容的方法,涉及curl的使用及正则匹配的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php正则匹配html中带class的div并选取其中内容的方法。分享给大家供大家参考。具体分析如下:

先看一段html代码:

复制代码 代码如下:

 
 

 
 
     潮汐表数据仅供参考
    
        
   
     
     
     
     
           
        
   
      
      
      
      
           
    

潮时 (Hrs)

00:58

05:20

13:28

21:15

潮高 (cm)

161

75

288

127

 
    

时区:-1000 (东10区)  潮高基准面:在平均海平面下174CM

 
      
 
 
 
 
 

这是源程序里边的一部分,为了容易看懂,删减了一大部分,只取

div块中的内容

首页先用file_get_content或curl获取内容部分,我用的是curl。

复制代码 代码如下:

$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="/

.*?/ism"; 
if(preg_match_all($regex4, $return, $matches)){ 
   print_r($matches); 
}else{ 
   echo '0'; 
}

这样就可以了,不多解释,懂PHP的看代码,打印看一下效果。

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!