adrianTNT Posted August 15, 2009 Share Posted August 15, 2009 Hello. I dont know how this works, I have many zip names with a timestamp in file name, like: animation_23432432.zip gallery_34232432432.zip What would be the code to return that number from such strings? something like $my_timestamp = "[anything]_%s.zip" ?! Quote Link to comment Share on other sites More sharing options...
adrianTNT Posted August 15, 2009 Author Share Posted August 15, 2009 I think I found it, but if anyone knows a better one please let me know. preg_match('/^(.*)_(.*).zip/', "animation_3393434349.zip", $matches); $timestamp_from_name = $matches[2]; echo $timestamp_from_name; Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 16, 2009 Share Posted August 16, 2009 You're creating unecessary group captures, the dot in .zip in your pattern could be anything (as the dot isn't escaped to be treated as a literal... ) There are alternative solutions (as usual): For example, you could just grab numbers that come before .zip like so: preg_match('#[0-9]+(?=\.zip)#', "animation_3393434349.zip", $matches); echo $timestamp_from_name = $matches[0]; Or, even simpler, if you know there is a series of numbers in the string (as assuming the blah_ part before the numbers don't contain numbers - much like in your sample string): preg_match('#[0-9]+#', "animation_3393434349.zip", $matches); echo $timestamp_from_name = $matches[0]; I'm sure there's many ways to accomplish this.. pick your poison. Quote Link to comment Share on other sites More sharing options...
Garethp Posted August 16, 2009 Share Posted August 16, 2009 I'd personally use '~[a-zA-Z]+_([0-9]+)\.zip$~ 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.