Jump to content


cutting the filename and let the extension stay.

  • Please log in to reply
4 replies to this topic

#1 bilis_money

  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 05 July 2006 - 07:59 AM

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

thanks in advance.

#2 Zane

  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 05 July 2006 - 08:19 AM

$filepath = "somefilename.jpgd"
//None Regex way
$extension = substr($filepath, (strrpos($filepath,".") * -1));
echo $extension;
//The Regex way
if(ereg(".*(\..*)", $filepath, $extension) {
  echo $extension[1];

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank


#3 Orio

  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 05 July 2006 - 09:05 AM

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:
echo $arr[$values-1]; //will echo "php"
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.

Think you're smarty?

(Gone until 20 to November)

#4 slipperyfish

  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 05 July 2006 - 09:16 AM

I wrote a fairly simple procedure to work it out:

$file = "test.jpg";
$extension = substr(strrchr($file, "."), 0);

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.

#5 bilis_money

  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 05 July 2006 - 11:22 AM

reply i guess my style is a little longer than you are guys.

thanks for the shorter codes.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users