[英]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.