Home > Backend Development > PHP Tutorial > Example of obtaining images and DIV content in web pages with php_PHP tutorial

Example of obtaining images and DIV content in web pages with php_PHP tutorial

WBOY
Release: 2016-07-13 10:27:36
Original
917 people have browsed it

Share the simple method of obtaining images and DIV content in web pages using php, which is all implemented through regular expressions.

1. Get all the pictures in the webpage:

<?<span>php 
</span><span>//</span><span>取得指定位址的內容,并储存至 $text </span>
<span>$text</span>=<span>file_get_contents</span>('http://www.jbxue.com/'<span>); 

</span><span>//</span><span>取得所有img标签,并储存至二维数组 $match 中 </span>
<span>preg_match_all</span>('/<img[^>]*>/i', <span>$text</span>, <span>$match</span><span>); 

</span><span>//</span><span>打印出match </span>
<span>print_r</span>(<span>$match</span><span>); 
</span>?>
Copy after login

2. Get the first picture on the webpage:

<?<span>php
</span><span>//</span><span>取得指定位址的內容,并储存至 $text </span>
<span>$text</span>=<span>file_get_contents</span>('http://www.jbxue.com/'<span>); 
</span><span>//</span><span>取得第一个 img 标签,并储存至二维数组 $match 中 </span>
<span>preg_match</span>('/<img[^>]*>/Ui', <span>$text</span>, <span>$match</span><span>);
</span><span>//</span><span>打印出match</span>
<span>print_r</span>(<span>$match</span><span>);
</span>?>
Copy after login

3. Get specific div block data in the specified web page:

<?<span>php
</span><span>//</span><span>取得指定位址的內容,并储存至 $text </span>
<span>$text</span>=<span>file_get_contents</span>('http://www.jbxue.com/'<span>); 
</span><span>//</span><span>去除换行及空白字符(序列化內容才需使用)
//$text=str_replace(array("/r","/n","/t","/s"), '', $text); 
//取出 div 标签且 id 为 PostContent 的內容,并储存至二维数组 $match 中 </span>
<span>preg_match</span>('/<div[^>]*id="PostContent"[^>]*>(.*?) <//div>/si',<span>$text</span>,<span>$match</span><span>);
</span><span>//</span><span>打印出match[0]</span>
<span>print</span>(<span>$match</span>[0<span>]);
</span>?>
Copy after login

4. Combination of 2 and 3 above:

<?<span>php 
</span><span>//</span><span>取得指定位址的內容,并储存至 $text </span>
<span>$text</span>=<span>file_get_contents</span>('http://www.jbxue.com/'<span>); 

</span><span>//</span><span>取出 div 标签且 id 为 PostContent 的內容,并储存至二维数组 $match 中 </span>
<span>preg_match</span>('/<div[^>]*id="PostContent"[^>]*>(.*?) <//div>/si',<span>$text</span>,<span>$match</span><span>); 

</span><span>//</span><span>取得第一个 img 标签,并储存至二维数组 $match2 中 </span>
<span>preg_match</span>('/<img[^>]*>/Ui', <span>$text</span>, <span>$match2</span><span>); 

</span><span>//</span><span>打印出match2[0] </span>
<span>print_r</span>(<span>$match2</span>[0<span>]); 
</span>?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/815972.htmlTechArticle Share the simple method of obtaining images and DIV content in web pages using php, which is all implemented through regular expressions. 1. Get all the pictures in the webpage: ? php // Get the content of the specified address and store it...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template