PHP从字符串获取图像的所有URL [英] PHP Get All URL's of Images From String

查看:189
本文介绍了PHP从字符串获取图像的所有URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要PHP的函数或Regex字符串,可以像这样传递字符串:

I need a function or Regex string for PHP that I can pass a string through like so:

Lorem ipsum dolor sit amet, http://www.gettyimages. com/images/marketing/frontdoorStill/PanoramicImagesRM/FD_image.jpg 顺势而为. Nullam sed diam lectus,一种轮状兽人.悬浮能力. Nulla facilisi.悬浮能力. ut http://www.handsonuniverse.org/get_images/images/20090802. ngc6992.HOS.jpg 毛茸茸的毛茸茸的坐着alit精英tristique坐amet laoreet浓香调味品. Lorem ipsum dolor坐下,一直保持良好状态. Aliquam euismod arcu non odio http://www.prelovac. com/vladimir/wp-content/uploads/2008/03/example.jpg aliquam前庭. Sed eleifendtellus id augue luctus acultrices leo semper.

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. Suspendisse potenti. Nulla facilisi. Suspendisse potenti. 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. 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.

我会得到回报:

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/vladimir/wp-content/uploads/2008/03/example.jpg

在一个数组中.我需要它来获取基于天气的URL,或者它们是否包含常规图像扩展名,例如* .jpg,*.png,*.bmp等.任何人都知道它的存在,因此我可以避免重新发明轮子吗?谢谢!

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? Thanks!

推荐答案

下面的示例适用于您:

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

添加您要捕获的其他任何扩展名.

Add whatever other extensions you want to capture.

请注意,上面的内容不一定强大(例如,它不匹配www.blah.com/image.jpg).即使它们是图像(即http://domain.com/blah.jpg?loadsmall=true之类),也不会匹配未在扩展名中结尾的URL.有多种方法可以使它更智能,但这实际上取决于您期望的输入类型,因为这将使您的解析变得多么复杂.

Note that the above is not necessarily robust (it would not match www.blah.com/image.jpg for example). 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). 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.

这篇关于PHP从字符串获取图像的所有URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆