DonEnzo
DonEnzo

Reputation: 1

How to remove the first 18 characters (date & time) from filename?

I need to remove the date and time (all characters until and including the "_") from JPG/PNG files in a folder. All the files look like this:

2019_07_31_150349_tcResultBig.png
2019_07_31_161349_dTResultBig.png
etc.

At the end the files should be renamed:

tcResultBig.png
dTResultBig.png
etc.

Reading all the files in a foreach-loop works quite well. But I struggle with using a regular expression and the replacing part.

I tried so far the following:

<?php
/* current directory */
$dir = ".";
/* find images in current directory and consider different combinations     of filename extensions: jpg, JPG, png, PNG */
$images = glob($dir."/*.[JjPp][PpNn][Gg]");

foreach($images as $image) {                
    preg_match("MY REGEX", $image);
        ....

}   
?>

Upvotes: 0

Views: 85

Answers (5)

zomega
zomega

Reputation: 2316

Because the length of the date/time is always the same you can simply use

substr($fname, 18);

If you have other files in your folder you could use

if ($fname[4] == "_" && substr($fname, -4) == ".png")

to check if it is a file you want to process.

If this is not sufficient because there could be files in the folder having a very similar name I recommend a regex. You could also use

explode($fname, "_")

and then check the first 4 parts with

ctype_digit()

. But regex is more beautiful.

You could also check if the date and time in the file name are valid. So 2019_02_29_... will be ignored because 2019 is not a leap year and so this file must be a different than the ones you want to process. I don't know if regex is capable of checking if a date is valid.

If the date/time is a local time (not UTC) where you have summer and winter time it will get more complex. In Germany on 31 March 2019 the time zone was changed from normal time zone to summer time zone. It means all clocks were changed from 02:00 to 03:00 at night. This means 31 March 2019 02:30 is not a invalid date/time for Germany. You might want to ignore such a file because it has no valid date/time. If the date/time in your file names have a fixed time zone you don't have to perform this check. :-)

Upvotes: 0

sam
sam

Reputation: 2984

You need to store the file names in an array, setting a variable will just overwrite the previous name. It's better to just use array_map which will store all the files name in a new array.

Non-loop method:

<?php
$dir = '.';
$images = glob("$dir/*.[JjPp][PpNn][Gg]");

$files = array_map(function($image) {
  $name = explode('_', $image);
  return $name[count($name) - 1];
}, $images);

Which will result in this:

Array ( [0] => tcResultBig.png [1] => dTResultBig.png ) 

Upvotes: 0

Will
Will

Reputation: 927

No need for regex or explode. If you want to remove the 18 first characters then use the substr() function :

$fileName = '2019_07_31_150349_tcResultBig.png';
$fileNameWithoutDate = substr($fileName, 18);

Upvotes: 2

Moses Schwartz
Moses Schwartz

Reputation: 3027

You don't need REGEX for this, You just need explode it by _ and then get the last one

foreach($images as $image) {                
   $xpl = explode('_', $image); 
   $newName = end($xpl);
}  

Upvotes: 1

dazed-and-confused
dazed-and-confused

Reputation: 1333

$str = "2019_07_31_150349_tcResultBig.png";
$parts = explode("_", $str);
$image = $parts[count($parts) - 1];

echo "Image: " . $image;

Upvotes: 2

Related Questions