Reputation: 340
I want to find the latest video in a html but this html doesn't sort well.
https://jsfiddle.net/8zk6j7dv/
<html><head><title>111.111.111.111 - /video8/</title></head><body><H1>111.111.111.111 - /video8/</H1><hr>
[To Parent Directory]
5/14/2020 11:26 AM 635 2020y-5m-14d-11h-20m-44s.mp4
5/14/2020 12:02 PM 30333149 2020y-5m-14d-11h-57m-38s.mp4
5/14/2020 12:21 PM 32048599 2020y-5m-14d-12h-16m-7s.mp4
5/14/2020 12:31 PM 31973846 2020y-5m-14d-12h-26m-6s.mp4
5/14/2020 12:41 PM 33358304 2020y-5m-14d-12h-36m-6s.mp4
5/14/2020 12:51 PM 29515351 2020y-5m-14d-12h-46m-6s.mp4
5/14/2020 1:01 PM 34340983 2020y-5m-14d-12h-56m-6s.mp4
5/14/2020 12:11 PM 31259022 2020y-5m-14d-12h-6m-6s.mp4
5/14/2020 1:21 PM 36133243 2020y-5m-14d-13h-16m-9s.mp4
5/14/2020 1:31 PM 38054158 2020y-5m-14d-13h-26m-6s.mp4
5/14/2020 1:36 PM 0 2020y-5m-14d-13h-36m-6s.mp4_audio
5/14/2020 1:38 PM 19232355 2020y-5m-14d-13h-36m-6s.mp4_video
5/14/2020 1:46 PM 36643767 2020y-5m-14d-13h-41m-6s.mp4
5/14/2020 1:56 PM 36170676 2020y-5m-14d-13h-51m-7s.mp4
The filenames don't zero padding 5m to 05m so I can't compare the time. I need to format them first.
$arr = ["2020y-1m-1d-0h-0m-0s",
"2020y-5m-20d-12h-10m-6s",
"2020y-5m-24d-12h-0m-6s",
"2020y-12m-31d-23h-50m-56s"
];
foreach ($arr as $key=>$value) {
$tt = preg_replace('/-(?<![0-9])(\d)(?![0-9])\w/', 'str_pad($1, 2, "0", STR_PAD_LEFT)', $value);
//$tt = preg_replace('/-(?<![0-9])(\d)(?![0-9])\w/', '0$1', $value);
echo $tt;
echo "\n\r";
}
The output should be
2020y-01m-01d-00h-00m-00s
2020y-05m-20d-12h-10m-06s
2020y-05m-24d-12h-00m-06s
2020y-12m-31d-23h-50m-56s
How can I fix the preg_replace?
https://regex101.com/r/HywlVw/1/
Upvotes: 0
Views: 141
Reputation: 47992
You don't need any capture groups.
Find: ~-\K(?=\d[dhms])~
Replace: 0
This means look for a hyphen, then forget you found it (\K
), then lookahead for a digit that is immediately followed by d
, h
, m
, or s
. Then you merely replace that zero-width position with a zero.
preg_replace('~-\K(?=\d[dhms])~', '0', $arr);
Upvotes: 1
Reputation: 6053
If you do want to use regex for this rather than strptime
and strftime
, you can use the following:
(?<=-)(\d)(?=[msdh])
https://regex101.com/r/kzhXF2/1
Upvotes: 1