1. 取得指定網頁內的所有圖片:
<?php
//取得指定位址的內容,並儲存至text
$text=file_get_contents('http://yourweb/');
//取得第一個img標簽,並儲存至陣列match(regex語法與上述同義)
preg_match('/<img[^>]*>/Ui', $text, $match);
//打印match
print_r($match);
-----------------
2. 取得指定網頁內的第一張圖片:
<?php
//取得指定位址的內容,並儲存至text
$text=file_get_contents('http://yourweb/');
//取得第一個img標簽,並儲存至陣列match(regex語法與上述同義)
preg_match('/<img[^>]*>/Ui', $text, $match);
//打印match
print_r($match);
------------------------------------
3. 取得指定網頁內的特定div區塊(藉由id判斷):
<?php
//取得指定位址的內容,並儲存至text
$text=file_get_contents('http://yourweb/');
//去除換行及空白字元(序列化內容才需使用)
$text=str_replace(array("\r","\n","\t","\s"), '', $text);
//取出div標簽且id為PostContent的內容,並儲存至陣列match
preg_match('/<div[^>]*id="PostContent"[^>]*>(.*?) <\/div>/si',$text,$match);
//打印match[0]
print($match[0]);
-------------------------------------------
4. 上述2及3的結合:
<?php
//取得指定位址的內容,並儲存至text
$text=file_get_contents('http://yourweb/');
//取出div標簽且id為PostContent的內容,並儲存至陣列match
preg_match('/<div[^>]*id="PostContent"[^>]*>(.*?) <\/div>/si',$text,$match);
//取得第一個img標簽,並儲存至陣列match2
preg_match('/<img[^>]*>/Ui', $match[0], $match2);
//打印match2[0]
print_r($match2[0]);
轉載請聲明來源!
