3v4l.org

run code in 300+ PHP versions simultaneously
<?php $tests = [ 'bar 3 m foo', # - 'bass drop 2 d bare', # - '2 3 m foo foo', # - '1 222 4 d baz', # - '5d 4h 3m 2s', # - '5 days 4 minutes', # - '9w8d7h6m5s', # - '2 d 3 m baz', # + '5d 4h 3m 2s foo', # + '2 s foo', # + '222 h baz bar', # + '1 mon foo', # + mon for month? '1w bar', # + '1 month baz', # + '2 months foobar', # + '4 months 2 months foo', # + repeating same string is allowed by strtotime, though it sums up https://3v4l.org/FIgNC '5m3s bar', # + '7mon6w5d4h3m2s bazinga!', # + '3 seconds 5 hours 5 minutes jeeey hoe :D damn I should sleep...' # + ]; /* * Replaces (s|m|h|d|w|mon) with strtotime compatible abbreviations * * It *should* allow these: https://gist.github.com/DaveRandom/625fb4bf73112474fb5768a0a300e4e5 * * NOTE: Below regex...uh, specially the (?&str) bit probably sucks... * * @param array $tests * @return array */ function normalizeTimeSpec($tests) { $reg1 = <<<'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 | .+ (*SKIP) ~uix REGEX; $array = []; foreach($tests as $case){ $output = preg_replace_callback ($reg1, function($matches){ if (!isset($matches['int'])) { 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; } return $matches['int'].$t; }, $case); $array[] = $output; } return $array; } /* * Test whether given time string passes regex rules (and show the matches array) * This is mainly for Reminders plugin where we allow trailing (but not leading) strings * * @param array $tests * @return array */ function testTimeSpec($tests){ $finalArray = []; # I saw another solution given by @bwoebi - https://3v4l.org/6BoW1 # However that doesn't fail any longer than "5 months 4 weeks" see ^ # pretty sure these can be improved (but it's late and I can't regex any more now...) $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)){ $finalArray[] = $matches['time'].$matches['string']; } } return $finalArray; } $normalized = normalizeTimeSpec($tests); $output = testTimeSpec(normalizeTimeSpec($tests)); print_r($normalized); print_r($output);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TI4eA
function name:  (null)
number of ops:  19
compiled vars:  !0 = $tests, !1 = $normalized, !2 = $output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
  142     1        INIT_FCALL                                               'normalizetimespec'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $4      
          4        ASSIGN                                                   !1, $4
  143     5        INIT_FCALL                                               'testtimespec'
          6        INIT_FCALL                                               'normalizetimespec'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $6      
          9        SEND_VAR                                                 $6
         10        DO_FCALL                                      0  $7      
         11        ASSIGN                                                   !2, $7
  144    12        INIT_FCALL                                               'print_r'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                                 
  145    15        INIT_FCALL                                               'print_r'
         16        SEND_VAR                                                 !2
         17        DO_ICALL                                                 
         18      > RETURN                                                   1

Function normalizetimespec:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 15
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/TI4eA
function name:  normalizeTimeSpec
number of ops:  18
compiled vars:  !0 = $tests, !1 = $reg1, !2 = $array, !3 = $case, !4 = $output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   37     1        ASSIGN                                                   !1, '%7E%0A%5Cs%3F+%28%3F%3Cint%3E+%5Cd%7B1%2C5%7D%29+%5Cs%3F%0A%28%3F%3Ctime%3E%0A++%28%3F%3A+s+%28%3F%3D%28%3F%3Aec%28%3F%3Aond%29%3Fs%3F%29%3F%28%3F%3A%5Cb%7C%5Cd%29%29%0A++++%7C+m+%28%3F%3D%28%3F%3Ain%28%3F%3Aute%29%3Fs%3F%29%3F%28%3F%3A%5Cb%7C%5Cd%29%29%0A++++%7C+h+%28%3F%3D%28%3F%3A%28%3F%3Aou%29%3Frs%3F%29%3F%28%3F%3A%5Cb%7C%5Cd%29%29%0A++++%7C+d+%28%3F%3D%28%3F%3Aays%3F%29%3F%28%3F%3A%5Cb%7C%5Cd%29%29%0A++++%7C+w+%28%3F%3D%28%3F%3Aeeks%3F%29%3F%28%3F%3A%5Cb%7C%5Cd%29%29%0A++++%7C+mon+%28%3F%3D%28%3F%3A%28%3F%3Ath%29%3Fs%3F%29%3F%28%3F%3A%5Cb%7C%5Cd%29%29%0A++%29%0A%29%0A%5B%5E%5Cd%5D%2A%3F%28%3F%3D%5Cb%7C%5Cd%29%0A%23+do+only+extract+start+of+string%0A%7C+.%2B+%28%2ASKIP%29%0A%7Euix'
   55     2        ASSIGN                                                   !2, <array>
   57     3      > FE_RESET_R                                       $7      !0, ->15
          4    > > FE_FETCH_R                                               $7, !3, ->15
   59     5    >   INIT_FCALL                                               'preg_replace_callback'
          6        SEND_VAR                                                 !1
   60     7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FTI4eA%3A60%240'
   85     8        SEND_VAL                                                 ~8
          9        SEND_VAR                                                 !3
         10        DO_ICALL                                         $9      
   59    11        ASSIGN                                                   !4, $9
   87    12        ASSIGN_DIM                                               !2
         13        OP_DATA                                                  !4
   57    14      > JMP                                                      ->4
         15    >   FE_FREE                                                  $7
   90    16      > RETURN                                                   !2
   91    17*     > RETURN                                                   null

End of function normalizetimespec

Function %00%7Bclosure%7D%2Fin%2FTI4eA%3A60%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
8 jumps found. (Code = 188) Position 1 = 20, Position 2 = 22, Position 3 = 24, Position 4 = 26, Position 5 = 28, Position 6 = 30, Position 7 = 32, Position 8 = 7
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 32
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 9, Position 2 = 20
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 22
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 24
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 26
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 28
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 30
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 30
Branch analysis from position: 28
Branch analysis from position: 26
Branch analysis from position: 24
Branch analysis from position: 22
Branch analysis from position: 20
filename:       /in/TI4eA
function name:  {closure}
number of ops:  37
compiled vars:  !0 = $matches, !1 = $t
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
   61     1        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      !0, 'int'
          2        BOOL_NOT                                         ~3      ~2
          3      > JMPZ                                                     ~3, ->5
   62     4    > > RETURN                                                   ''
   64     5    >   FETCH_DIM_R                                      ~4      !0, 'time'
          6      > SWITCH_STRING                                            ~4, [ 's':->20, 'm':->22, 'h':->24, 'd':->26, 'w':->28, 'mon':->30, ], ->32
   65     7    >   CASE                                                     ~4, 's'
          8      > JMPNZ                                                    ~5, ->20
   68     9    >   CASE                                                     ~4, 'm'
         10      > JMPNZ                                                    ~5, ->22
   71    11    >   CASE                                                     ~4, 'h'
         12      > JMPNZ                                                    ~5, ->24
   74    13    >   CASE                                                     ~4, 'd'
         14      > JMPNZ                                                    ~5, ->26
   77    15    >   CASE                                                     ~4, 'w'
         16      > JMPNZ                                                    ~5, ->28
   80    17    >   CASE                                                     ~4, 'mon'
         18      > JMPNZ                                                    ~5, ->30
         19    > > JMP                                                      ->32
   66    20    >   ASSIGN                                                   !1, '+sec+'
   67    21      > JMP                                                      ->32
   69    22    >   ASSIGN                                                   !1, '+min+'
   70    23      > JMP                                                      ->32
   72    24    >   ASSIGN                                                   !1, '+hour+'
   73    25      > JMP                                                      ->32
   75    26    >   ASSIGN                                                   !1, '+day+'
   76    27      > JMP                                                      ->32
   78    28    >   ASSIGN                                                   !1, '+week+'
   79    29      > JMP                                                      ->32
   81    30    >   ASSIGN                                                   !1, '+month+'
   82    31      > JMP                                                      ->32
         32    >   FREE                                                     ~4
   84    33        FETCH_DIM_R                                      ~12     !0, 'int'
         34        CONCAT                                           ~13     ~12, !1
         35      > RETURN                                                   ~13
   85    36*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FTI4eA%3A60%240

Function testtimespec:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 17
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 16
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 16
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/TI4eA
function name:  testTimeSpec
number of ops:  20
compiled vars:  !0 = $tests, !1 = $finalArray, !2 = $regex, !3 = $case, !4 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  100     0  E >   RECV                                             !0      
  101     1        ASSIGN                                                   !1, <array>
  106     2        ASSIGN                                                   !2, '++++%2F%28%3F%28DEFINE%29%0A++++++%28%3F%3Cint%3E%0A++++++++%28%5Cs%2A%5Cb%29%3F%0A++++++++%28%5Cd%7B1%2C5%7D%29%3F%0A++++++++%28%5Cs%2A%29%3F%0A++++++%29%0A++++++%28%3F%3Ctimepart%3E%0A++++++++%28%3F%26int%29%0A++++++++%28+s%28ec%28ond%29%3Fs%3F%29%3F%0A++++++++%7C+m%28in%28ute%29%3Fs%3F%7Conths%3F%29%3F%0A++++++++%7C+h%28rs%3F%7Cours%3F%29%3F%0A++++++++%7C+d%28ays%3F%29%3F%0A++++++++%7C+w%28eeks%3F%29%3F%0A++++++++%29%0A++++++++%5Cb%0A++++++%29%0A+++++%29%0A++++%0A+++++%5E%28%3F%3Ctime%3E%0A+++++++%28%3F%3A%28%3F%26timepart%29%28%2ASKIP%29.%29%2B%0A+++++%29%0A+++++%28%3F%3Cstring%3E.%2B%29%24%0A+++++%2Fuix'
  132     3      > FE_RESET_R                                       $7      !0, ->17
          4    > > FE_FETCH_R                                               $7, !3, ->17
  134     5    >   INIT_FCALL                                               'preg_match'
          6        SEND_VAR                                                 !2
          7        SEND_VAR                                                 !3
          8        SEND_REF                                                 !4
          9        DO_ICALL                                         $8      
         10      > JMPZ                                                     $8, ->16
  135    11    >   FETCH_DIM_R                                      ~10     !4, 'time'
         12        FETCH_DIM_R                                      ~11     !4, 'string'
         13        CONCAT                                           ~12     ~10, ~11
         14        ASSIGN_DIM                                               !1
         15        OP_DATA                                                  ~12
  132    16    > > JMP                                                      ->4
         17    >   FE_FREE                                                  $7
  139    18      > RETURN                                                   !1
  140    19*     > RETURN                                                   null

End of function testtimespec

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.75 ms | 1411 KiB | 22 Q