bilis_money Posted July 5, 2006 Share Posted July 5, 2006 yo :oi know this is easy.Could you show me the snippet of codes for bothnone 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 codingstyle.thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/13710-cutting-the-filename-and-let-the-extension-stay/ Share on other sites More sharing options...
Zane Posted July 5, 2006 Share Posted July 5, 2006 $filepath = "somefilename.jpgd"[code]//None Regex way$extension = substr($filepath, (strrpos($filepath,".") * -1));echo $extension;//The Regex wayif(ereg(".*(\..*)", $filepath, $extension) { echo $extension[1];}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13710-cutting-the-filename-and-let-the-extension-stay/#findComment-53220 Share on other sites More sharing options...
Orio Posted July 5, 2006 Share Posted July 5, 2006 @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. Quote Link to comment https://forums.phpfreaks.com/topic/13710-cutting-the-filename-and-let-the-extension-stay/#findComment-53235 Share on other sites More sharing options...
slipperyfish Posted July 5, 2006 Share Posted July 5, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13710-cutting-the-filename-and-let-the-extension-stay/#findComment-53240 Share on other sites More sharing options...
bilis_money Posted July 5, 2006 Author Share Posted July 5, 2006 reply i guess my style is a little longer than you are guys.thanks for the shorter codes. Quote Link to comment https://forums.phpfreaks.com/topic/13710-cutting-the-filename-and-let-the-extension-stay/#findComment-53268 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.