/    Sign up×
Community /Pin to ProfileBookmark

Preg_match for image files?

[CODE]preg_match(‘/https?://[a-zA-Z0-9._-]{1,253}/.*.w{3,4}/’,$thumb)[/CODE]

This can still match almost anything. How can I preg_match just images? Is there a PHP function for this?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 19.2010 — Depends on the context and what it is exactly that you want to do. For instance, if you're "screen-scraping" an HTML page for all IMG tags and their SRC attributes, I'd use the [url=http://php.net/dom]DOM extension[/url] instead of hoping I can come up with a regexp that will match every possibility without giving false positives.
Copy linkTweet thisAlerts:
@narutodude000authorJul 19.2010 — There's a field where an user can submit a thumbnail. I want to preg_match it to see if it's an image.
Copy linkTweet thisAlerts:
@NogDogJul 19.2010 — If they are submitting a URL, the surest way I can think of would be to use [url=http://php.net/curl]cURL[/url] to check the Content-Type header to see if it's a known image type. (Use the CURLOPT_NOBODY option to only get the header.) If you go with a regexp, you have to account not just for .jpg, .jpeg, .png, etc., but also anything that could be used to serve up an image, such as .php, .aspx, etc. that could also be non-images.
×

Success!

Help @narutodude000 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...