Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding last .

Posted on 2003-11-17
8
Medium Priority
?
357 Views
Last Modified: 2008-03-06
What is the best way to find the last occurence of a . in a file name?

So like if my user submits My.File.zip how would I find the . right before the zip?

The reason I'm asking (maybe there is a different answer to this) is because I want to be able to accept odd file types that aren't defined in PHP like models, .mdl. But PHP identifies this is as application/octet-stream along with any other file types it doesn't understand.

Thanks in advance.
0
Comment
Question by:drakkarnoir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 2

Expert Comment

by:neele
ID: 9768715
u can use strrpos to get last occurance of string.

e.g.
$str = "morning";
echo strpos($str, "m"); //answer is 0

$str = "morning morning";
echo strpos($str, "m"); //answer is 8

hope it helps
0
 
LVL 2

Accepted Solution

by:
neele earned 1000 total points
ID: 9768744
i forget to say u can then use substr (Sub String)
e.g

$str = "funny.xyz";

$newFilename = substr($str, 0, strpos ($str, ".")).".zip"; //start from 0 end with 8

will give u funny.zip

Hope it helps U
0
 

Author Comment

by:drakkarnoir
ID: 9768915
I'm trying to find the last occurence of a . not trying to concatnate them :\
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Assisted Solution

by:Muhammad Wasif
Muhammad Wasif earned 1000 total points
ID: 9769097
If u have the file name u can get the portion after the last . by

$local_file = "wasif.txt.tz";
$ext_array =explode(".",$local_file);
$last_position = count($ext_array) - 1  ;
$extension = $ext_array[$last_position] ;

echo $extension;

it will return  tz

and u r geting the uploaded files in array userfile[]  u can get the filetype by

$_FILES['userfile']['type'][0]

WASIF
0
 
LVL 2

Expert Comment

by:neele
ID: 9769152
SIGN ;c U din read my posting well ...

strrpos -> is FIND last occurance !!  STRRPOS

** Opps i put wrong just now lack a "R"
0
 
LVL 2

Expert Comment

by:neele
ID: 9769169
So once again U can
e.g.

$str = "hello.xxx.yyy.zzz";
//using
strrpos($str, ".");  //GIVE u the INDEX of LAST occurance ! in this case is 13

I give u the concatnate portion is to help U with the coding in replacing the last e.g. .zzz with your appropriate extension. Thought it might help U. ...

0
 

Author Comment

by:drakkarnoir
ID: 9772073
Ah thank you very much, both of you. Sorry neele, I did not understand your solution but now I do :)
0
 
LVL 2

Expert Comment

by:neele
ID: 9775521
OK welcome :) maybe i din state well initially..
glad u get it, n solve ur issue. thanks for accepting the solution as well !
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question