Pedro Chagas
asked on
Get the Extension of the Image from URL
Hi E's, I need to get the extension of the image from a URL address (gif, png, jpg etc).
In practice if I have this address: http://example.com/image/image.gif I want the output $extension = gif.
How I get the image extension from the URL?
Regards, JC
In practice if I have this address: http://example.com/image/image.gif I want the output $extension = gif.
How I get the image extension from the URL?
Regards, JC
$ext = preg_match('/(?<=\.)[a-z]+ /i', $input)
Correction:
$ext = preg_match('/(?<=\.)[a-z]+ $/i', $input)
$ext = preg_match('/(?<=\.)[a-z]+
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> A flawless mode:
What about ".jpeg"?
What about ".jpeg"?
What about:
$x = explode(".",$path);
$ext = array_pop($x);
$x = explode(".",$path);
$ext = array_pop($x);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Right
substr($path, (strlen($path)-3), strlen($path));
It´s flawless for most common extensions, jpeg ext is very rare in web apps
substr($path, (strlen($path)-3), strlen($path));
It´s flawless for most common extensions, jpeg ext is very rare in web apps
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> It´s flawless for most common extensions
It won't cover Google's new WEBP extension either ;)
It won't cover Google's new WEBP extension either ;)
Tthe proposed code by kumaranmca will only work if your file name has only one "." in it. you need to grab the end of the array to be sure you get the extension. A filename like:
Jim.Smith.jpg
would not give the desired results by taking $file_extension[1].
Jim.Smith.jpg
would not give the desired results by taking $file_extension[1].
ASKER
Hi to all, first thanks for participation on this question.
@kaufmed, your solution in post 33827856, the return is allways "1":
========================== ========== ========== ========
$input = "http://portugal.montranet.com/portugal/palacioestoi/8.jpg";
$ext = preg_match('/(?<=\.)[a-z]+ $/i', $input);
echo $ext;
========================== ========== ========== ========
@shanikawm, your both codes working very well.
@betopa, your code in post 33828117 work well too
@smadeira, your both codes work well too.
@kumaranmca, your solution work too.
Please don't post more solutions. @kaufmed you can give me a new solution (I appreciate the fact you send solution by mobile phone)!
Regards, JC
@kaufmed, your solution in post 33827856, the return is allways "1":
==========================
$input = "http://portugal.montranet.com/portugal/palacioestoi/8.jpg";
$ext = preg_match('/(?<=\.)[a-z]+
echo $ext;
==========================
@shanikawm, your both codes working very well.
@betopa, your code in post 33828117 work well too
@smadeira, your both codes work well too.
@kumaranmca, your solution work too.
Please don't post more solutions. @kaufmed you can give me a new solution (I appreciate the fact you send solution by mobile phone)!
Regards, JC
ASKER
The bug for close questions in mod multiple solutions persists:
I want close the question and assign the points:
33828045 100 points
33828069 75 "
33828117 50 "
33828189 50 "
33829752 25 "
Total = 300 points
I want close the question and assign the points:
33828045 100 points
33828069 75 "
33828117 50 "
33828189 50 "
33829752 25 "
Total = 300 points
My pattern was correct; my use of preg_match was not. Forgive me, I'm not a php programmer :\
The correct usage is:
The correct usage is:
$input = "http://portugal.montranet.com/portugal/palacioestoi/8.jpg";
preg_match('/(?<=\.)[a-z]+$/i', $input, $ext);
echo $ext[0];
ASKER
Hi @kaufmed, your solution working too. Unfortunately I have been close the question before you post your correct solution, so I cant assign points to you.
Thanks.
JC
Thanks.
JC
>> Unfortunately I have been close the question before you post your correct solution, so I cant assign points to you.
It's cool. I knew that before I posted. I added it for the benefit of future EE readers :)
It's cool. I knew that before I posted. I added it for the benefit of future EE readers :)