emehrkay Posted December 11, 2006 Share Posted December 11, 2006 i have never written any expressions (its on the top, maybe third (its definitley behind mastering oo javascript), of my list of things to learn), but i just want to write a method in my upload class that would add _mmddyyyy to my file name. filename_xxx.ext where _xxx is what im addingi could explode by period, loop through ad it before the extension and all that jazz or use substring, strchr etc, but im sure there is a simple regex way.any help?thank you Quote Link to comment Share on other sites More sharing options...
c4onastick Posted December 11, 2006 Share Posted December 11, 2006 [code]$file = "filename.ext";$date = date('mdY');$newfile = preg_replace('/(\.ext)$/', "_$date$1", $file);print_r($newfile);[/code]Or you can hard-code the date into the $date variable. Depending on your extension structure, you could add some alternation:[code]preg_replace('/(\.(?:ext|jpg|exe|dll|dat|doc|xls))$/', "_$date$1", $file)[/code]or if you have files like "filename.dat1, filename.dat2, filename.dat3..."[code]preg_replace('/(\.dat[0-9])$/', "_$date$1", $file)[/code]Regex will let you be pretty specific. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 12, 2006 Author Share Posted December 12, 2006 wow thanks for your reply, but what if i dont know the extension. is there a way for me to say 'whatever is after the last period is the extension'? i wont even try to guess or hack it using the carrot or dollar sign Quote Link to comment Share on other sites More sharing options...
c4onastick Posted December 12, 2006 Share Posted December 12, 2006 In that case, you have a couple of options. Since I really have no idea what you're dealing with, this is the most general solution I can come up with at the moment.[code]$newfile = preg_replace('/(\.[^.]+)$/', "_$date$1", $file);[/code]You might be able to get away with this, but its a risky venture. It will match things like:[code]filename.extfile.htmlnew.season.0301.avifile.datblah.c[/code]Which is what you want. But it'll also match things like:[code].bashrc.bash_profile[/code]So as long as your file list is good, you shouldn't have any problems.Glad to help! Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 12, 2006 Author Share Posted December 12, 2006 thank you sir. it will be controlled (up to the programmer) so hopefully they will not use it to rename files that they do not need renamed Quote Link to comment Share on other sites More sharing options...
c4onastick Posted December 13, 2006 Share Posted December 13, 2006 Not a problem, glad to help! Quote Link to comment 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.