1

このような名前のファイルがたくさんあります...

full-file(1).jpg
full-file(10).jpg
full-file(11).jpg
full-file(12).jpg
full-file(2).jpg
etc...

PHPを使用してこれらすべてのファイルの名前を変更し、このように名前を変更する最も効率的な方法を見つけようとしています...

full-file0001.jpg
full-file0010.jpg
full-file0011.jpg
full-file0012.jpg
full-file0002.jpg

フォルダーからすべてのファイルを読み取り、それらをループすることまではできましたが、括弧を削除して先頭の 0 で数字を 4 桁にする最良の方法についてはわかりません。

$image_files = get_files($thumbpath);
foreach($image_files as $index=>$file) {
    echo $file;
}   
4

6 に答える 6

4

正規表現を使用して数字を取得し、次を使用してゼロを埋めますsprintf()

$image_files = get_files($thumbpath);
foreach($image_files as $index=>$file) {
  // Capture \d+ into $matches[1]
  preg_match('/\((\d+)\)/', $file, $matches);
  // Pad it with %04d in sprintf()
  $newfile = sprintf("full-file%04d.jpg", $matches[1]);
} 

例:

php > $file = 'full-file(12).jpg';
php > preg_match('/\((\d+)\)/', $file, $matches);
php > $newfile = sprintf("full-file%04d.jpg", $matches[1]);
php > echo $newfile;
// full-file0012.jpg

更新 (より柔軟なファイル名の場合):

反対票を投じた人を喜ばせるために、より柔軟なファイル名が必要だとしか思えないので、正規表現を展開します。

$image_files = get_files($thumbpath);
foreach($image_files as $index=>$file) {
  preg_match('/([^(]+)\((\d+)\)(.+)/', $file, $matches);
  $newfile = sprintf("%s%04d%s", $matches[1], $matches[2], $matches[3]);

  // And rename the file
  if (!rename($file, $newfile)) {
    echo "Could not rename $file.\n";
  }
  else echo "Successfully renamed $file to $newfile\n";
}

パターンは最初に一致し、最初の までのすべてが に一致(([^(]+)、その後に 経由で番号が続き、(\d+)残りはすべて経由で一致し(.*)ます。

于 2012-06-29T11:48:10.430 に答える
2

オススメしている方をまだ見たことがありsscanf()ません。

<?php

$files = array(
  "full-file(1).jpg",
  "full-file(10).jpg",
  "full-file(11).jpg",
  "full-file(12).jpg",
  "full-file(2).jpg",
);

foreach ($files as $file) {
  $n = sscanf($file, "full-file(%d).jpg");
  printf("full-file%04d.jpg\n", $n[0]);
}

戻り値:

full-file0001.jpg
full-file0010.jpg
full-file0011.jpg
full-file0012.jpg
full-file0002.jpg

もちろん、これは「full-file」がファイルの実際の名前である場合にのみ機能します。sscanf() は正規表現パーサーではなく、printf() スタイルのフォーマット文字列を使用してデータを抽出するだけですが、 http://php.net/sscanfに記載されているよりも高度なフォーマット認識を行います。他のファイル名を処理する必要がある場合は、フォーマット文字列を拡張できます。

<?php

$files = array(
  "this-file(1).jpg",
  "full-file(10).jpg",
  "foo(11).jpg",
  "blarg(12).jpg",
  "full-file(2).jpg",
);

foreach ($files as $file) {
  $n = sscanf($file, "%[a-z-](%d).jpg");
  printf("%s%04d.jpg\n", $n[0], $n[1]);
}

戻り値:

this-file0001.jpg
full-file0010.jpg
foo0011.jpg
blarg0012.jpg
full-file0002.jpg
于 2012-06-29T13:31:38.140 に答える
2

REGEXP (括弧を削除) と文字列パディング (4 桁を強制する) を組み合わせて使用​​できます。

注: 置換コールバックを使用して、両方の操作を 1 か所で行います。

$files = array(
    'full-file(1).jpg',
    'full-file(10).jpg',
    'full-file(11).jpg',
    'full-file(12).jpg',
    'full-file(2).jpg'
);

function pr_callback($match) {
    return str_pad($match[1], 4, '0', STR_PAD_LEFT);
}
foreach($files as $file)
    echo preg_replace_callback('/\((\d+)\)/', pr_callback, $file).'<br />';

出力:

full-file0001.jpg
full-file0010.jpg
full-file0011.jpg
full-file0012.jpg
full-file0002.jpg

于 2012-06-29T11:49:45.393 に答える
1

使用コード:

preg_match('/^(.*?)\((\d+)\)(.*)$/', $name, $m);
$name = sprintf("%s%04d%s", $m[1], $m[2], $m[3]);

ここでそれを見てテストしてください

于 2012-06-29T12:08:46.463 に答える
1

str-pad()が必要です。もうすぐサンプル…

EDIT 1str_pad : と を使用したソリューションpreg_replace_callback
OBS : 匿名関数は php5.3+ のみ。

foreach ($image_files as $file)
{
    $o = preg_replace_callback(
            "|\((\d+)\)|", function($matches)
            {
                $r = str_pad($matches[1], 4, '0', STR_PAD_LEFT);
                return $r;
            }
            , $file);

    echo $o . "\n";
}
于 2012-06-29T11:47:13.830 に答える
1

ファイルが実際にはフルファイル(0000) と呼ばれていないと仮定します。jpg :

<?php 
$arr = array('full-file(1).jpg',
             'full-file(10).jpg',
             'full-file(11).png',
             'full-file(12).jpg',
             'full-file(2).gif',
             'abc(12345).jpg',
             'def(99).jpg',
             'xzy-file(100).jpg');

function format_filename($matches){
  return $matches[1].sprintf("%04d",$matches[3]).'.'.$matches[5];
}
function process_array(&$value){
    $value = preg_replace_callback('/^(.*?)(\()(\d+)(\)).(jpg|png|gif)/','format_filename',$value);
}

array_walk($arr,'process_array');

print_r($arr);
/*
Array
(
    [0] => full-file0001.jpg
    [1] => full-file0010.jpg
    [2] => full-file0011.png
    [3] => full-file0012.jpg
    [4] => full-file0002.gif
    [5] => abc12345.jpg
    [6] => def0099.jpg
    [7] => xzy-file0100.jpg
)
*/
?>
于 2012-06-29T12:00:33.183 に答える