且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-22 22:12:51

下面的示例适用于您:

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.