Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx - How To Insert String Before File Extension

Tags:

regex

php

How would I insert "_thumb" into files that are being dyanmically generated.

For example, I have a site that allows users to upload an image. The script takes the image, optimizes it and saves to file. How would I make it insert the string "_thumb" for the optimized image?

I'm currently saving 1 version of the otpimized file. ch-1268312613-photo.jpg

I want to save the original as the above string, but want to append, "_thumb" like the following string ch-1268312613-photo_thumb.jpg

like image 624
st4ck0v3rfl0w Avatar asked Mar 12 '10 00:03

st4ck0v3rfl0w


1 Answers

No need to use regex. This will do it:

$extension_pos = strrpos($filename, '.'); // find position of the last dot, so where the extension starts $thumb = substr($filename, 0, $extension_pos) . '_thumb' . substr($filename, $extension_pos); 
like image 170
Chad Birch Avatar answered Sep 21 '22 08:09

Chad Birch