如何捕获和归档文件名
我已经尝试在 PHP 中使用preg_replace_callback,但我不知道如何正确使用。
function upcount_name_callback($matches) {
//var_export($matches);
$index = isset($matches[3]) ? intval($matches[3]) + 1 : 1;
return '_' . $index;
}
$filename1 = 'news.jpg';
echo preg_replace_callback('/^(([^.]*?)(?:_([0-9]*))?)(?:\.|$)/', 'upcount_name_callback', $filename1, 1);
$filename2 = 'aw_news_2.png';
echo preg_replace_callback('/^(([^.]*?)(?:_([0-9]*))?)(?:\.|$)/', 'upcount_name_callback', $filename2, 1);
输出(错误):
array (
0 => 'news.',
1 => 'news',
2 => 'news',
3 => '1',
)
_1jpg <= wrong - filename1
.
array (
0 => 'aw_news_2.',
1 => 'aw_news_2',
2 => 'aw_news',
3 => '2',
)
_3png <= wrong - filename2
输出(右):
news_1 <= filename1
.
aw_news_3 <= filename2