[英]Detect and extract image url from text and html tags
How can i detect if there is some image html tag inside a text and extract just the url of the image ? 如何检测文本中是否存在某些图像html标记并仅提取图像的网址?
Eg. 例如。
Extract this url : 提取此网址:
http://
www.someurl.com/somefileprocessor.php/12345/somedir/somesubdir/someniceimage.j
pg
from this tag (this tag can be inside another bunch of text and/or html) 来自此标记(此标记可以在另一堆文本和/或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" />
Thank's in advance Ângelo 感谢提前Ângelo
You can use CRUL
to get content and then extract all img
tags from content. 您可以使用CRUL
获取内容,然后从内容中提取所有img
标记。 to get data by curl
: 通过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;
}
then use regular expression to extract data. 然后使用正则表达式提取数据。
^https?://(?:[a-z\-]+\.)+[a-z]{2,6}(?:/[^/#?]+)+\.(?:jpg|gif|png)$
this helps you to extract all image urls(in img tag or not). 这有助于您提取所有图像网址(在img标签中或不是)。
If you need crawler ,you can use PHPCrawl 如果您需要抓取工具,可以使用PHPCrawl
Thank's a lot for the awnswers, as i learn some more PHP. 感谢awnswers,因为我学习了更多的PHP。 I try this quick and dirty way, it also extracts the image url 我尝试这种快速而肮脏的方式,它也提取图像网址
$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.