Reputation: 913
I'm getting xml files generated daily and renamed to a specific structure like as follows:
03-17-2017 02 05 09 PM - (20000) - Item 1.xml
03-17-2017 02 06 14 PM - (20001) - Item 2.xml
03-17-2017 02 06 19 PM - (20002) - Item 3.xml
03-17-2017 02 08 03 PM - (20003) - Item 4.xml
03-17-2017 02 08 53 PM - (20004) - Item 5.xml
03-17-2017 02 09 41 PM - (20002) - Item 3.xml
As you can see, I sometimes get duplicates, such as 20002
, with a different timestamp.
Is there a way to programatically exclude this and only include unique numbers in my retrieval? The filename structure is always the same and the number is always a 6 digit number.
Right now I am using glob to return the files in the directory
$files = glob("03-17-2017/*.xml");
if (is_array($files)) {
foreach($files as $filename) {
//
}
Is there a function or method in being selective to only retrieve the filenames with a unique number?
Upvotes: 1
Views: 31
Reputation: 92854
Two approaches to get the needed result:
-- using strpos
and substr
functions:
$files = [];
foreach (glob("03-17-2017/*.xml") as $f) {
$start_pos = strpos($f, "(") + 1;
$num = substr($f, $start_pos, strpos($f, ')') - $start_pos);
if (!isset($files[$num])) $files[$num] = $f;
}
print_r($files);
-- using preg_match
function:
$files = [];
foreach (glob("03-17-2017/*.xml") as $f) {
preg_match("/\((\d{5})\)/", $f, $m); // capturing the number
$num = $m[1];
if (!isset($files[$num])) $files[$num] = $f;
}
Upvotes: 1