I have a program that calculates the amount of Days, Hours, Minutes or Seconds that are given but it is only capable of taking in 1 argument such as "1d" = 86400 seconds.
Now I want it to calculate when receiving "1d 3h 16m 2s" and convert it to the amount of seconds. Also when given a wrong input, I want it to tell the user something like "wrong input".
How could I make that work?
This is the code I have so far (which works for a single value) — now I need to modify it for the extended input data
<?php
switch (substr((string)$argv[1], -1)) {
case "d":
echo ((int)$argv[1] * 86400);
break;
case "h":
echo ((int)$argv[1] * 3600);
break;
case "m":
echo ((int)$argv[1] * 60);
break;
case "s":
echo ((int)$argv[1] * 1);
break;
}
?>
2
Answers
To match several unit time I used
preg_match_all()
function and then use a switch iteration for each time unit. Calculate the seconds for each unit and sum it.PHP online : https://onlinephp.io/c/f7d8c
Assuming you don’t have any higher units to parse and the units are always in big-endian order, you can use a single regex patter to parse all of data in the string, then use simple arithmetic to calculate the seconds.
The regex will attempt to capture the number of each optional unit. Even if a given unit does not exist, the matches array will still hold the place so that the elements are always consistently placed. The subpatterns are rather repetious so this will make it easier to extend/reduce if needed.
Code: (Demo)
If your input data is consistently formatted and always contains each unit, then the process is much simpler because
sscanf()
can parse the numbers directly to integers. (Demo)If units are not guaranteed to exist or may exist in any order, you can use this functional-style snippet to parse the expression. It isolates each number-unit pair, leverages a lookup array to multiply the number by the appropriate factor, then sum the calculations. (Demo)
As for signalling to the user that an invalid expression has been passed in, you could check for a falsey return valud from
preg_match()
,sscanf()
, orpreg_match_all()
before processing further. If one of thepreg_
functions returns0
, then something went wrong. Ifsscanf()
returnsfalse
, then something went wrong. You will need to clarify if1d 3d
is an invalid input as well as expressing how variable your data is allowed to be before we can confidently offer advice on the validation.