简体   繁体   English

PHP从字符串中获取图像的所有URL

[英]PHP Get All URL's of Images From String

I need a function or Regex string for PHP that I can pass a string through like so: 我需要PHP的函数或Regex字符串,我可以传递一个字符串,如下所示:

Lorem ipsum dolor sit amet, http://www.gettyimages.com/images/marketing/frontdoorStill/PanoramicImagesRM/FD_image.jpg consectetur adipiscing elit. Lorem ipsum dolor sit amet, http ://www.gettyimages.com/images/marketing/frontdoorStill/PanoramicImagesRM/FD_image.jpg consectetur adipiscing elit。 Nullam sed diam lectus, a rutrum orci. Nullam sed diam lectus,rutrum orci。 Suspendisse potenti. Suspendisse的潜力。 Nulla facilisi. Nulla facilisi。 Suspendisse potenti. Suspendisse的潜力。 Ut http://www.handsonuniverse.org/get_images/images/20090802.ngc6992.HOS.jpg ullamcorper mauris sit amet elit tristique sit amet laoreet nunc condimentum. Ut http://www.handsonuniverse.org/get_images/images/20090802.ngc6992.HOS.jpg ullamcorper mauris sit amet elit tristique sit amet laoreet nunc condimentum。 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor坐下来,精致的adipistur elit。 Aliquam euismod arcu non odio http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg aliquam vestibulum. Aliquam euismod arcu non odio http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg aliquam vestibulum。 Sed eleifend tellus id augue luctus ac ultrices leo semper. Sed eleifend tellus id augue luctus ac ultrices leo semper。

And I would would get in return get: 我会得到回报得到:

http://www.gettyimages.com/images/marketing/frontdoorStill/PanoramicImagesRM/FD_image.jpg http://www.handsonuniverse.org/get_images/images/20090802.ngc6992.HOS.jpg http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg http://www.gettyimages.com/images/marketing/frontdoorStill/PanoramicImagesRM/FD_image.jpg http://www.handsonuniverse.org/get_images/images/20090802.ngc6992.HOS.jpg http://www.prelovac。 COM /弗拉基米尔/可湿性粉剂内容/上传/ 2008/03 / example.jpg

in an array. 在数组中。 I need it to grab the URL's based on weather or not they contain regular image extensions, such as *.jpg, *.png, *.bmp, etc. Anyone know one that exists so I can avoid reinventing the wheel? 我需要它来抓取基于天气的URL,或者它们包含常规的图像扩展名,例如* .jpg,* .png,* .bmp等。任何人都知道一个存在,所以我可以避免重新发明轮子? Thanks! 谢谢!

Well, below will work for your example: 那么,下面将适用于您的示例:

preg_match_all('/(https?:\/\/\S+\.(?:jpg|png|gif))\s+/', $content, $matches);

Add whatever other extensions you want to capture. 添加您要捕获的任何其他扩展名。

Note that the above is not necessarily robust (it would not match www.blah.com/image.jpg for example). 请注意,上述内容不一定非常强大(例如,它与www.blah.com/image.jpg不匹配)。 Nor would it match URLs that did not end in the extension, even if they were images (ie, http://domain.com/blah.jpg?loadsmall=true or something). 它也不会匹配未在扩展中结束的URL,即使它们是图像(即http://domain.com/blah.jpg?loadsmall=true或其他东西)。 There are ways to make it more smart, but it really depends on what sort of input you are expecting, as that would drive how complex your parsing needs to be. 有一些方法可以使它更加智能,但它实际上取决于您期望的输入类型,因为这会推动您的解析需要多么复杂。

If you don't want do this with regular expressions. 如果您不想使用正则表达式执行此操作。 Instead, parse the HTML. 而是解析HTML。

<?php
$html='YOUR_STRING';
$dom = new domDocument; 
$dom->loadHTML($html); 
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');

foreach ($images as $image) 
   {   
     echo $image->getAttribute('src'); 
   }

?>

Here's the regex: /(http|https|ftp|ftps)\\:\\/\\/[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(\\/\\S*)?/g 这是正则表达式: /(http|https|ftp|ftps)\\:\\/\\/[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(\\/\\S*)?/g

Demo: http://regexr.com?31ni5 演示: http//regexr.com?31ni5

Credits go to some random Google results. 积分转到谷歌的一些随机结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM