php_guest Posted May 2, 2009 Share Posted May 2, 2009 there are 4 allowed types of images: jpg, jpeg, png, gif. I want to change the name of each image, but the extension must be the kept. How to remove everything what is before .jpg or .jpeg. or .png or .gif? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/156468-how-to-change-the-name-of-images-with-different-extensions/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 preg_replace('#.*\.(.*)$#', '.' . $1, $img); ? Quote Link to comment https://forums.phpfreaks.com/topic/156468-how-to-change-the-name-of-images-with-different-extensions/#findComment-823906 Share on other sites More sharing options...
php_guest Posted May 2, 2009 Author Share Posted May 2, 2009 I just found something easier to do what I want: function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } Quote Link to comment https://forums.phpfreaks.com/topic/156468-how-to-change-the-name-of-images-with-different-extensions/#findComment-823910 Share on other sites More sharing options...
php_guest Posted May 2, 2009 Author Share Posted May 2, 2009 Even easier, just simple pathinfo('$filename'). Sorry to not check this before I put the question. So I put this code to change image name to put it into database: $ext = pathinfo('$filename'); $len = 16; $base='ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz123456789'; $max=strlen($base)-1; $string=''; mt_srand((double)microtime()*1000000); while (strlen($string)<$len+1) $string.=$base{mt_rand(0,$max)}; $newname=$string.".".$ext['extension']; Quote Link to comment https://forums.phpfreaks.com/topic/156468-how-to-change-the-name-of-images-with-different-extensions/#findComment-823922 Share on other sites More sharing options...
Daniel0 Posted May 3, 2009 Share Posted May 3, 2009 php_guest, it's no longer needed to seed the PRNG in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/156468-how-to-change-the-name-of-images-with-different-extensions/#findComment-824913 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.