Page 1 of 1

Find last period in string? Count? Explode?

Posted: Wed Aug 20, 2008 2:18 pm
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?

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

Posted: Wed Aug 20, 2008 2:42 pm
by nowaydown1

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

Posted: Wed Aug 20, 2008 2:56 pm
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";
 

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

Posted: Wed Aug 20, 2008 6:35 pm
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.