Jump to content

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


adrianTNT

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.