Jump to content

file name is formated like anim_45354453.zip how to get the number?


Recommended Posts

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" ?!

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. ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.