3v4l.org

run code in 300+ PHP versions simultaneously
<?php $tests = [ 'bar 3 m foo -', '2 s foo +', '222 h baz bar +', 'bass drop 2 d bare -', '2 3 m foo foo -', '1 222 4 d baz -', '2 d 3 m baz +', '5d 4h 3m 2s foo +', '5d 4h 3m 2s', '1 mon foo', '1w bar', '1 month baz', '2 months foobar', '4 months 2 months foo' //repeating same string is allowed by strtotime, though it sums up ]; /* * Replaces (s|m|h|d|w|mon) with strtotime compatible abbreviations * @param array $tests * @return array */ function normaliseTimeSpec($tests) { $reg1 = "/ (?<int> (?:\G|(?!\n)) \s*\b \d{1,5} \s* )\K (?<time> (?: s|m(on)?|h|d|w)\b ) /uix"; $array = []; foreach($tests as $case){ $out = preg_replace_callback ($reg1, function($matches){ 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; } return $t; }, $case); $array[] = $out; } return $array; } /* * Test whether given time string passes regex rules & match * @param array $tests * @return array */ function testTimeSpec($tests){ $finalArray = []; $regex = <<<'REGEX' /(?(DEFINE) (?<int> (\s*\b)? (\d{1,5})? (\s*)? ) (?<timepart> (?&int) ( s(ec(ond)?s?)? | m(in(ute)?s?|onths?)? | h(rs?|ours?)? | d(ays?)? | w(eeks?)? ) \b ) ) ^(?<time> (?:(?&timepart)(*SKIP).)+ ) (?<string>.+)$ /uix REGEX; foreach($tests as $case){ if(preg_match($regex, $case, $matches)){ $final[] = $matches['time'].$matches['string']; } } return $final; } $output = testTimeSpec(normaliseTimeSpec($tests)); print_r($output);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => 2 sec foo + [1] => 222 hour baz bar + [2] => 2 day 3 min baz + [3] => 5day 4hour 3min 2sec foo + [4] => 1 month foo [5] => 1week bar [6] => 1 month baz [7] => 2 months foobar [8] => 4 months 2 months foo )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
42.44 ms | 1567 KiB | 4 Q