分享下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>?>
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>?>
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>?>
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>?>
Statement
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
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks agoByDDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks agoByDDD
Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation
1 months agoByDDD
Where to find the Crane Control Keycard in Atomfall
3 weeks agoByDDD
Roblox: Dead Rails - How To Complete Every Challenge
3 weeks agoByDDD

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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