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

php获取网页中图片、DIV内容的简单方法

WBOY
Release: 2016-06-06 20:21:18
Original
1939 people have browsed it

这篇文章主要介绍了php获取网页中图片、DIV内容的简单方法,都是通过正则表达式实现的,强大的正则啊,需要的朋友可以参考下

1、获取网页中所有的图片:

复制代码 代码如下:


//取得指定位址的內容,并储存至 $text 
$text=file_get_contents('http://www.jb51.net/');   
 
//取得所有img标签,,并储存至二维数组 $match 中  
preg_match_all('/php获取网页中图片、DIV内容的简单方法]*>/i', $text, $match);  
 
//打印出match  
print_r($match);  
?>

2、获取网页中的第一张图片:

复制代码 代码如下:


//取得指定位址的內容,并储存至 $text 
$text=file_get_contents('http://www.jb51.net/'); 

//取得第一个 img 标签,并储存至二维数组 $match 中  
preg_match('/php获取网页中图片、DIV内容的简单方法]*>/Ui', $text, $match);

//打印出match
print_r($match);
?>

3、获取指定网页中特定的 div 区块数据:

复制代码 代码如下:


//取得指定位址的內容,并储存至 $text  
$text=file_get_contents('http://www.jb51.net/'); 

//去除换行及空白字符(序列化內容才需使用)
//$text=str_replace(array("/r","/n","/t","/s"), '', $text);  

//取出 div 标签且 id 为 PostContent 的內容,并储存至二维数组 $match 中  
preg_match('/

]*id="PostContent"[^>]*>(.*?) /div>/si',$text,$match);

//打印出match[0]
print($match[0]);
?>

4. 上述2及3的結合:

复制代码 代码如下:


//取得指定位址的內容,并储存至 $text  
$text=file_get_contents('http://www.jb51.net/');     
 
//取出 div 标签且 id 为 PostContent 的內容,并储存至二维数组 $match 中  
preg_match('/

]*id="PostContent"[^>]*>(.*?) /div>/si',$text,$match);  
 
//取得第一个 img 标签,并储存至二维数组 $match2 中  
preg_match('/php获取网页中图片、DIV内容的简单方法]*>/Ui', $text, $match2);  
 
//打印出match2[0]  
print_r($match2[0]);  
?>
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!