简体   繁体   English

PHP-根据文件名模式查找最新文件

[英]PHP - Find latest file based on filename pattern

I have a bunch of gamers data. 我有一堆玩家数据。 The data is stored as json with filename pattern as gamerid_date_time_seed.json . 数据存储为json,文件名模式为gamerid_date_time_seed.json Eg below: 例如:

8841-2018-05-03-09-56-14-2118-data.json
8841-2018-05-03-10-50-22-1568-data.json
8841-2018-05-04-04-36-51-6081-data.json
8841-2018-05-04-06-56-50-1100-data.json

In the first filename for example, 2018-05-03-09-56-14 is the year-month-date-hour-min-sec format. 例如,在第一个文件名中, 2018-05-03-09-56-14year-month-date-hour-min-sec格式。 8841 is the gamer id and the last 4 digits, 2118 in this case is a seed value that can be ignored. 8841是玩家ID,最后4位数字,在这种情况下, 2118是可以忽略的种子值。

How can I parse this so that I get only the latest data file of a gamer? 我该如何解析,以便仅获得玩家的最新数据文件? (In this case 8841-2018-05-04-06-56-50-1100-data.json) The issue is that sometimes a gamer might have multiple files but for same day. (在这种情况下为8841-2018-05-04-06-56-50-1100-data.json)问题是有时游戏玩家可能有多个文件但同一天。

I am not very good with regular expressions and stuff so I would really appreciate some help here. 我对正则表达式和其他东西不太满意,因此在此我将不胜感激。

You can use the glob function to get an array of files, and reverse sort to get the newest file as the first item in your array. 您可以使用glob函数获取文件数组,并进行反向排序以获取最新文件作为数组中的第一项。

$gamerid = 8441;
$files = glob("filepath/".$gamerid."-*.json");
rsort($files);
$newestFile = $files[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM