0

I have a folder that stores some thousands of pictures files. I want to change the name of each file that matches the condition. The idea is if the file name has _10 change to _ten, if has _5 change to _five. So, xxdf23_10hy.jpg should be xxdf23_tenhy.jpg, 16_5_gt5.jpg should change to 16_five_gt5.jpg. But if file mane is gdjd_7.jpg, do nothing.

The code is working good, but it is matching the string ".." that should not be matched.

This is the part of the code:

$photoPath="../pic/";
$dir = new DirectoryIterator($photoPath);
foreach ($dir as $fileinfo) {
    $filename = $fileinfo->getFilename();
    if(preg_match($filename, "/_10/")){
      //change te name to "_ten"
    }
    elseif(preg_match($filename, "/_5/")){
      //change te name to "_five"
    }
}

Something is not good with the way I am using the preg_match function. But if I try it inside regex tester it works good.

What am I missing?

6
  • 1
    This is a good example of where you really don't need regexps. Just a simple check if the string contains a substring would do. Commented Nov 11, 2015 at 17:35
  • @SamiKuhmonen internally, substring uses regexps. So with regexp you save an step. And regexp are powerful, you can make a regexp in this case for bulletproof (actually it doesnt) Commented Nov 11, 2015 at 17:36
  • @MarcosPérezGude I know PHP is silly, but if that's true... Commented Nov 11, 2015 at 17:38
  • silly why? Regular expresions are powerful, an PHP too. Maybe you are a Java programmer, that makes ids in html with dots.... LOL Commented Nov 11, 2015 at 17:40
  • @MarcosPérezGude I don't believe this to be the case, but would like to see if you have documentation on this. My understanding is that the underlying algorithm that PHP uses is the Sunday algorithm iti.fh-flensburg.de/lang/algorithmen/pattern/sundayen.htm Commented Nov 11, 2015 at 17:52

2 Answers 2

3

You've got your subject and pattern switched in the preg_match() commands. Try this:

if (preg_match("/_10/", $filename)) {
    // more code
}

http://php.net/manual/en/function.preg-match.php

Sign up to request clarification or add additional context in comments.

Comments

1

No need for the overhead of regex here at all. Perhaps simple glob() and str_replace() would meet your needs.

$photoPath="../pic/";

$replacements = array(
    '_5' => '_five',
    '_10' => '_ten'
);

foreach ($replacements as $pattern => $replace) {
    $files = glob($photoPath . '*' . $pattern . '*');
    foreach($files as $file) {
       $old_name = $file;
       $new_name = str_replace($pattern, $replace, $old_name); 
       rename($old_name, $new_name);
    }
}

Here we don't even use regex or PHP string searching functionality to find the files we want to change. We use glob() which is basically a direct call to underlying libc glob() function and should perform significantly better and with less memory usage than the DirectoryIterator with post-filter functionality you are currently using. DirectoryIterator is probably overkill here anyway unless you are doing more complex file operations. glob() would filter your file names for you, meaning you are not doing useless regex searches against every file contained in DirectoryIterator object like you are currently doing.

The actual filepath name change is executed using basic str_replace(). You don't currently show how you are doing this, but I would imagine you would implement something similar or possibly just use preg_replace() rather than preg_match() if you desire to stick with regex approach.

2 Comments

This is another nice possiblity too. Thanks
@zwitterion Thanks. I did add some additional commentary contrasting my approach vs. the DirectoryIterator and regex approach. Hopefully this helps illustrate why I proposed this approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.