<?php
$trailingString = '7mon6w5d4h3m2s bazinga!';
$leadingString = 'bazinga! in 9w8d7h6m5s';
$both = 'foo in 9d8h5m bar';
$noText = '2 w 3 m 4 d 9 mon';
/*
* Replaces (s|m|h|d|w|mon) with strtotime compatible abbreviations
* Optionally allows leading and/or trailing strings and returns an array with separated result set
*
* It should allow these and alikes: https://gist.github.com/DaveRandom/625fb4bf73112474fb5768a0a300e4e5
*
* @param string $input, bool $requireLeadingString, bool $requireTrailingString
* @return array $array
*/
function getTimeSpec(string $input, bool $requireLeadingString = false, bool $requireTrailingString = false): array
{
$expr1 = "/(.*)\s+(?:in)\s+(.*)/ui";
$expr2 = <<<'REGEX'
~
\s? (?<int> \d{1,5}) \s?
(?<time>
(?: s (?=(?:ec(?:ond)?s?)?(?:\b|\d))
| m (?=(?:in(?:ute)?s?)?(?:\b|\d))
| h (?=(?:(?:ou)?rs?)?(?:\b|\d))
| d (?=(?:ays?)?(?:\b|\d))
| w (?=(?:eeks?)?(?:\b|\d))
| mon (?=(?:(?:th)?s?)?(?:\b|\d))
)
)
[^\d]*?(?=\b|\d)
# do only extract start of string
| (?<text> .+)
~uix
REGEX;
$finalArray = $parsedTimeSpec = [];
$leading = $trailing = "";
# If leading text is required, it should separate time and text by "in"
# ie. "foo in 9 weeks 5 days"
if($requireLeadingString && preg_match($expr1, $input, $parts)){
$leading = $parts['1'] ?? '';
$input = $parts['2'];
}
$time = preg_replace_callback ($expr2,
function($matches) use (&$leading, &$trailing, &$requireTrailingString, &$parsedTimeSpec) {
if ($requireTrailingString && isset($matches['text'])) {
$trailing = trim($matches['text']);
return '';
}
switch($matches['time']){
case 's':
$t = ' sec ';
break;
case 'm':
$t = ' min ';
break;
case 'h':
$t = ' hour ';
break;
case 'd':
$t = ' day ';
break;
case 'w':
$t = ' week ';
break;
case 'mon':
$t = ' month ';
break;
}
$completeTimeString = $matches['int'].$t;
$parsedTimeSpec["$t"] = $matches['int'];
return $completeTimeString;
}, $input);
if ($time != "") {
$finalArray['time'] = $parsedTimeSpec;
}
if ($trailing != "") {
$finalArray['trailing'] = $trailing;
}
if ($leading != "") {
$finalArray['leading'] = $leading;
}
return $finalArray;
}
$timeSpecWithLeadingString = getTimeSpec($leadingString, true);
$timeSpecWithTrailingString = getTimeSpec($trailingString, false, true);
$timeSpecWithBoth = getTimeSpec($both, true, true);
$timeSpecWithoutText = getTimeSpec($noText, false, false);
print_r($timeSpecWithLeadingString);
print_r($timeSpecWithTrailingString);
print_r($timeSpecWithBoth);
print_r($timeSpecWithoutText);
- Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- Array
(
[time] => Array
(
[ week ] => 9
[ day ] => 8
[ hour ] => 7
[ min ] => 6
[ sec ] => 5
)
[leading] => bazinga!
)
Array
(
[time] => Array
(
[ month ] => 7
[ week ] => 6
[ day ] => 5
[ hour ] => 4
[ min ] => 3
[ sec ] => 2
)
[trailing] => bazinga!
)
Array
(
[time] => Array
(
[ day ] => 9
[ hour ] => 8
[ min ] => 5
)
[trailing] => bar
[leading] => foo
)
Array
(
[time] => Array
(
[ week ] => 2
[ min ] => 3
[ day ] => 4
[ month ] => 9
)
)
- Output for 5.6.0 - 5.6.40
- Fatal error: Default value for parameters with a class type hint can only be NULL in /in/VjGWm on line 17
Process exited with code 255.
preferences:
201.69 ms | 401 KiB | 293 Q