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
Solved

Finding last .

Posted on 2003-11-17
8
350 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 250 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 20

Assisted Solution

by:Muhammad Wasif
Muhammad Wasif earned 250 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 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