0

I have a variable called image_name. example below

$image_name = "hello.png";

I want to cut the string when it see's the dot. Because I want to add a randomised int so the name cant be duplicated. For example at the moment in my code I have

$image_name = substr($image_name, 0, 10) . time() . rand(0, 9999);

and with the test string above it would return something like

hello.png13953322416647

My aim is have the string name equal to hello13953322416647.png - showing the .png after all timestamp and random number

So does anyone know how cut the string when it hits a dot in a string and then add it at the end..?

Thanks guys

  • Have you tried using `substr($image_name, 0, strrpos($image_name, '.')). time().rand(0, 9999).substr($image_name, strrpos($image_name, '.'))` ? – Diamondo25 Mar 20 '14 at 16:29

1 Answers1

4

I would use the pathinfo function. http://php.net/manual/en/function.pathinfo.php

$path_detail = pathinfo( $image_name );
$new_image_path = sprintf( "%s%s%s%s", $path_detail['filename'], time(), rand( 0,9999), $path_detail['extension']);