Jump to content

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.

$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]
@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.
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.