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 and return it in an array with the remaining text * * It *should* allow these: https://gist.github.com/DaveRandom/625fb4bf73112474fb5768a0a300e4e5 * * @param array $tests * @return array */ function separateAndNormalizeTimeSpec($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 | (?<text> .+) ~uix REGEX; $array = []; foreach($tests as $case){ $text = ""; $time = preg_replace_callback ($reg1, function($matches) use (&$text) { if (isset($matches['text'])) { $text = 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; } return $matches['int'].$t; }, $case); if ($time != "" && $text != "") { $array[] = [$time, $text]; } } return $array; } $normalized = separateAndNormalizeTimeSpec($tests); print_r($normalized);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0kdvf
function name:  (null)
number of ops:  9
compiled vars:  !0 = $tests, !1 = $normalized
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   94     1        INIT_FCALL                                               'separateandnormalizetimespec'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
   95     5        INIT_FCALL                                               'print_r'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

Function separateandnormalizetimespec:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 24
Branch analysis from position: 5
2 jumps found. (Code = 46) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 23
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 23
Branch analysis from position: 18
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/0kdvf
function name:  separateAndNormalizeTimeSpec
number of ops:  27
compiled vars:  !0 = $tests, !1 = $reg1, !2 = $array, !3 = $case, !4 = $text, !5 = $time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   35     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+%28%3F%3Ctext%3E+.%2B%29%0A%7Euix'
   53     2        ASSIGN                                                   !2, <array>
   55     3      > FE_RESET_R                                       $8      !0, ->24
          4    > > FE_FETCH_R                                               $8, !3, ->24
   56     5    >   ASSIGN                                                   !4, ''
   57     6        INIT_FCALL                                               'preg_replace_callback'
          7        SEND_VAR                                                 !1
   58     8        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F0kdvf%3A58%240'
          9        BIND_LEXICAL                                             ~10, !4
   84    10        SEND_VAL                                                 ~10
         11        SEND_VAR                                                 !3
         12        DO_ICALL                                         $11     
   57    13        ASSIGN                                                   !5, $11
   86    14        IS_NOT_EQUAL                                     ~13     !5, ''
         15      > JMPZ_EX                                          ~13     ~13, ->18
         16    >   IS_NOT_EQUAL                                     ~14     !4, ''
         17        BOOL                                             ~13     ~14
         18    > > JMPZ                                                     ~13, ->23
   87    19    >   INIT_ARRAY                                       ~16     !5
         20        ADD_ARRAY_ELEMENT                                ~16     !4
         21        ASSIGN_DIM                                               !2
         22        OP_DATA                                                  ~16
   55    23    > > JMP                                                      ->4
         24    >   FE_FREE                                                  $8
   91    25      > RETURN                                                   !2
   92    26*     > RETURN                                                   null

End of function separateandnormalizetimespec

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

End of function %00%7Bclosure%7D%2Fin%2F0kdvf%3A58%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.9 ms | 1407 KiB | 20 Q