Question

I am looking to determine the best way to dig out a portion of a file name. I'm putting together an application that needs to be able to explode a file name and then query a database based on a portion of that file name. Let me give you some examples of how the file name may come in.

Station-[_A]_Warren_2014-01-2614_40_01_377000.mp3
Fire_station_2014-02-1311_10_01_347000.mp3
FIRE_EMS_2014-03-0111_11_01_212000.mp3
Really_long_name_goes_here_2014-04-0123_12_01_177000.mp3

Right now, my variable after scanning a directory to find the long file name is $file. I need to figure out a way to possible explode those pieces (which I can do the explode) but I need a good way to help determine the "front" of that file name. The front of that file name is defined as "anything before the YYYY-MM-DDHH" format.

So I am guessing that there has to be some sort of regex or pre_match done on this?

Était-ce utile?

La solution

You can use preg_match

preg_match('/(.*)(_\d{4}-\d+-\d+)_(.*)/', $file, $matches);

echo $matches[1];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top