0

次のファイル構造を検討してください。

/folder/locaux-S04_3.html
/folder/blurb.txt
/folder/locaux-S04_2.html
/folder/locaux-S05_1.html
/folder/tarata.02.jpg
/folder/locaux-S04_1.html
/folder/dfdsf.pdf

ディレクトリ内の最大の数値を含む名前のファイルを取得する必要があります。上記の例では、locaux-S05_1.html です。

locaux-S*.html ファイルのみを取得する効率的な方法として glob() を思いつきましたが、次のステップで行き詰まっています。ファイル名に最高の値が含まれているファイルを見つけることです。

$files= glob(LOCAUX_FILE_PATH.'/locaux-S*.html');

foreach($files as $key=> $value){
    // loop through and get the value in the filename. Highest wins a trip to download land!

$end = strrpos($value,'.');
$len= strlen($value);
$length = $len-$end;
$str = substr($value,8,$length);
// this gives me the meat, ex: 03_02. What next?

}

どんなポインタでも大歓迎です。

4

2 に答える 2

2

これを試して:

$files = glob(LOCAUX_FILE_PATH.'/locaux-S*.html');
$to_sort = array();

foreach ($files as $filename)
{
    if (preg_match('/locaux-S(\d+)_(\d+)\.html/', $filename, $matches)) {
        $to_sort[$matches[1].'.'.$matches[2]] = $filename;
    }
}

krsort($to_sort);
echo reset($to_sort); // Full filepath of locaux-S05_1.html in your example

フロートを配列キーとして使用できないため、おそらく誰かがこれに基づいて構築できます(整数に変換されますが、これは良くありません)。私はまた、あなたが最初にアンダースコアの前の番号で並べ替え、次に 2 番目の番号を 2 番目の順序基準として使用します。

于 2010-10-06T22:14:35.987 に答える
1

もっと簡単な方法を見つけました:

$files= glob(LOCAUX_FILE_PATH.'/locaux-S*.html');
sort($files); // sort the files from lowest to highest, alphabetically
$file  = array_pop($files); // return the last element of the array
于 2010-10-06T22:18:07.700 に答える