php获取网页中图片与DIV内容实例
分享下php获取网页中图片、DIV内容的简单方法,都是通过正则表达式实现的。
1、获取网页中所有的图片:
<?<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 [^ alt="php获取网页中图片与DIV内容实例" >]*>/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、获取网页中的第一张图片:
<?<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 [^ alt="php获取网页中图片与DIV内容实例" >]*>/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、获取指定网页中特定的 div 区块数据:
<?<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. 上述2及3的結合:
<?<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 [^ alt="php获取网页中图片与DIV内容实例" >]*>/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
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
What's New in Windows 11 KB5054979 & How to Fix Update Issues
4 weeks ago
By DDD
How to fix KB5055523 fails to install in Windows 11?
3 weeks ago
By DDD
InZoi: How To Apply To School And University
1 months ago
By DDD
How to fix KB5055518 fails to install in Windows 10?
3 weeks ago
By DDD
Where to find the Site Office Key in Atomfall
1 months ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
