Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Finding last .

Posted on 2003-11-17
8
Medium Priority
?
358 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …
Suggested Courses

772 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