2015-11-11 17:30


  • regex
  • php

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:

$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?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享