如何检测文本中是否存在某些图像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
答案 0 :(得分:2)
快速尝试<img/>
标记特定的正则表达式:
preg_match_all('/<img[^>]*?\s+src\s*=\s*"([^"]+)"[^>]*?>/i', $str, $matches);
答案 1 :(得分:0)
您可以使用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
答案 2 :(得分:0)
感谢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);