Jump to content

Archived

This topic is now archived and is closed to further replies.

bilis_money

cutting the filename and let the extension stay.

Recommended Posts

yo  :o

i know this is easy.

Could you show me the snippet of codes for both
none REGEX and REGEX alike of this scenario.

let say i want to get the extension filename of
'myfile.gif' and not the filename itself but merely the extension
--> .gif.

what is your style? please show me this on none REGEX and REGEX coding
style.

thanks in advance.

Share this post


Link to post
Share on other sites
$filepath = "somefilename.jpgd"
[code]//None Regex way
$extension = substr($filepath, (strrpos($filepath,".") * -1));
echo $extension;
//The Regex way
if(ereg(".*(\..*)", $filepath, $extension) {
  echo $extension[1];
}[/code]

Share this post


Link to post
Share on other sites
@zanus-
I have a little problem with your Non-Regex way.
Let's say the file is called: hello.zanus.jpg it is possible, right?
You script will return zanus.jpg which is no extension...
So I think explode is the best here:
[hr]
[code=php:0]//$filename="hello.to.everyone.php"
$arr=explode(".",$filename);
$values=count($arr);
echo $arr[$values-1]; //will echo "php"
[/code]
[hr]
Basicly this splits the filename to parts (stores in a array) where it finds a dot (in our case it's 0=>hello, 1=>to, 2=>everyone, 3=>php). Then we check how many values there are (with count()) and then echo the last item in the array.

Orio.

Share this post


Link to post
Share on other sites
I wrote a fairly simple procedure to work it out:

[CODE]
$file = "test.jpg";
$extension = substr(strrchr($file, "."), 0);
[/CODE]

that returns ".jpg". It's not ideal, but it works fine. And if you have an extension with 4 digits (eg jpeg) it still allows you to run IFs or/we you want on it.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.