dongshanyan0322
2015-11-11 17:30这个正则表达式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:
$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?
- 点赞
- 回答
- 收藏
- 复制链接分享
2条回答
为你推荐
- 正则表达式从自定义代码创建HTML
- regex
- html
- php
- 3个回答
- 使用正则表达式和php从html中提取javascript对象
- regex
- parsing
- php
- 4个回答
- 获取带有动态内容的html源代码来进行正则表达式分析
- curl
- php
- jquery
- 1个回答
- 如何使用正则表达式在PHP代码中捕获不带引号的数组索引并引用它们?
- regex
- php
- 2个回答
- PHP:正则表达式在大括号之间提取任何东西
- php
- 1个回答
换一换