分享下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内容实例_PHP教程" >]*>/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内容实例_PHP教程" >]*>/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内容实例_PHP教程" >]*>/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>?>