Find last period in string? Count? Explode?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Find last period in string? Count? Explode?

Post by JAB Creations »

I've put together a an image upload script of sorts and I want to rename the avatar image to $userid.'_'.$username.'_avatar'.$extension; however I know for a fact that someone sooner or later is going to upload my_picture.something.look_at.all.the.periods!......jpg and so I need to find the last period starting right to left in order to retain the file extension (GIF, JPG, PNG, etc) unless someone has a better method for approaching this?
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Find last period in string? Count? Explode?

Post by nowaydown1 »

dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: Find last period in string? Count? Explode?

Post by dajawu »

Doubt you need it now, but heres some code just in case:

Code: Select all

 
$test = "hello.my.name.is.jpg";
echo "Input: $test <br>";
$num = strrpos($test,".");
$file = substr($test, 0, $num);
$ext = substr($test, $num, strlen($test));
echo "File: $file <br>";
echo "EXT: $ext";
 
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Find last period in string? Count? Explode?

Post by JAB Creations »

Awesome thanks! I think I'll run a quick check for two or more periods and then run that script as a function in such circumstances.
Post Reply