[英]Detect and extract image url from text and html tags
如何檢測文本中是否存在某些圖像html標記並僅提取圖像的網址?
例如。
提取此網址:
http://
www.someurl.com/somefileprocessor.php/12345/somedir/somesubdir/someniceimage.j
pg
來自此標記(此標記可以在另一堆文本和/或html中)
<img title="Some nice title" border="0"
hspace="0" alt="some useful hint" src="http://
www.someurl.com/somefileprocessor.php/12345/somedir/somesubdir/someniceimage.j
pg" width="629" height="464" />
感謝提前Ângelo
快速嘗試<img/>
標記特定的正則表達式:
preg_match_all('/<img[^>]*?\s+src\s*=\s*"([^"]+)"[^>]*?>/i', $str, $matches);
您可以使用CRUL
獲取內容,然后從內容中提取所有img
標記。 通過curl
獲取數據:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
然后使用正則表達式提取數據。
^https?://(?:[a-z\-]+\.)+[a-z]{2,6}(?:/[^/#?]+)+\.(?:jpg|gif|png)$
這有助於您提取所有圖像網址(在img標簽中或不是)。
如果您需要抓取工具,可以使用PHPCrawl
感謝awnswers,因為我學習了更多的PHP。 我嘗試這種快速而骯臟的方式,它也提取圖像網址
$imageurl = strstr($title, 'src',FALSE);
$imageurl = strstr($imageurl,'"',FALSE);
$imageurlpos = strpos($imageurl,'"');
$imageurl = substr($imageurl,$imageurlpos+1);
$imageurlpos = strpos($imageurl,'"');
$imageurl = substr($imageurl,0,$imageurlpos);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.