Find last period in string? Count? Explode?
Moderator: General Moderators
- 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?
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?
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";
- 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?
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.