[英]How to sort the list of files name and write them in a JSON using php?
[英]Using Regex to sort through a list of files
我正在编写一个脚本来对文件列表进行排序,获取与特定文件名匹配的最新文件,并将其复制到另一个文件夹。 我正在用 PHP 编写它,然后使用 CRON 将其设置为每天在特定时间运行。
文件命名如下: VS-order-export-105.xml VS-order-export-104.xml current-VS-order-export-105,xml current-VS-order-export.104.xml
我需要获取这些文件的最新版本,格式为“order-export-(number).xml”。 那些前缀为“current-”的应该被忽略。
我似乎无法让正则表达式正确地做到这一点。 这是我所拥有的:
<?php $src = '/public_html/folder/exports/';
$files_src = scandir($src, SCANDIR_SORT_DESCENDING);
foreach($files_src as $file) {
if (preg_match('/(^VS-), (.*)/A', $file) && !is_dir($src . $file)) {
$newest_file_src = $files_src[1];
$newest_file_path_src = $src . $files_src[1];
$dest_file_path = '/public_html/orders/';
rename($newest_file_path_src, $dest_file_path);
}
} ?>
请有人指出我做错了什么吗?
提前致谢!
在您的模式中,您试图匹配一个逗号后跟一个不存在的空格。
对于更具体的模式,您可以使用
\AVS-order-export-\d+\.xml\z
\\A
字符串开头VS-order-export-
字面匹配\\d+\\.xml
匹配 1+ 位数字和.xml
\\z
字符串结束例如
if (preg_match('/\AVS-order-export-\d+\.xml\z/', $file)) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.