3v4l.org

run code in 300+ PHP versions simultaneously
<?php $tests = [ 'bar 3 m foo', // - '5s 4m', // - '5s 10mins 4 days', // - 'bass drop 2 d bare', // - '2 3 m foo foo', // - '1 222 4 d baz', // - '5d 4h 3m 2s', // - '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 ]; /* * Replaces (s|m|h|d|w|mon) with strtotime compatible abbreviations * * ps. (?&int) is there so it doesn't replace the 's' in 'months' etc. * * @param array $tests * @return array */ function normalizeTimeSpec($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)){ $finalArray[] = $matches['time'].$matches['string']; } } return $finalArray; } print_r(normalizeTimeSpec($tests)); print_r(testTimeSpec(normalizeTimeSpec($tests)));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6Ftpb
function name:  (null)
number of ops:  17
compiled vars:  !0 = $tests
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
  102     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'normalizetimespec'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
  103     7        INIT_FCALL                                               'print_r'
          8        INIT_FCALL                                               'testtimespec'
          9        INIT_FCALL                                               'normalizetimespec'
         10        SEND_VAR                                                 !0
         11        DO_FCALL                                      0  $4      
         12        SEND_VAR                                                 $4
         13        DO_FCALL                                      0  $5      
         14        SEND_VAR                                                 $5
         15        DO_ICALL                                                 
         16      > 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/6Ftpb
function name:  normalizeTimeSpec
number of ops:  18
compiled vars:  !0 = $tests, !1 = $reg1, !2 = $array, !3 = $case, !4 = $out
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   31     1        ASSIGN                                                   !1, '%2F%28%3F%3Cint%3E+%28%3F%3A%5CG%7C%28%3F%21%0A%29%29+%5Cs%2A%5Cb+%5Cd%7B1%2C5%7D+%5Cs%2A+%29%5CK%0A++++++++++++++%28%3F%3Ctime%3E+%28%3F%3A+s%7Cm%28on%29%3F%7Ch%7Cd%7Cw%29%5Cb+%29%0A+++++++++++++%2Fuix'
   35     2        ASSIGN                                                   !2, <array>
   37     3      > FE_RESET_R                                       $7      !0, ->15
          4    > > FE_FETCH_R                                               $7, !3, ->15
   39     5    >   INIT_FCALL                                               'preg_replace_callback'
          6        SEND_VAR                                                 !1
   40     7        DECLARE_LAMBDA_FUNCTION                          ~8      [0]
   50     8        SEND_VAL                                                 ~8
          9        SEND_VAR                                                 !3
   39    10        DO_ICALL                                         $9      
         11        ASSIGN                                                   !4, $9
   52    12        ASSIGN_DIM                                               !2
         13        OP_DATA                                                  !4
   37    14      > JMP                                                      ->4
         15    >   FE_FREE                                                  $7
   55    16      > RETURN                                                   !2
   56    17*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
8 jumps found. (Code = 188) Position 1 = 16, Position 2 = 18, Position 3 = 20, Position 4 = 22, Position 5 = 24, Position 6 = 26, Position 7 = 28, Position 8 = 3
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 28
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 5, Position 2 = 16
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 18
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
1 jumps found. (Code = 42) Position 1 = 28
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
Branch analysis from position: 18
Branch analysis from position: 16
filename:       /in/6Ftpb
function name:  {closure}
number of ops:  31
compiled vars:  !0 = $matches, !1 = $t
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1        FETCH_DIM_R                                      ~2      !0, 'time'
          2      > SWITCH_STRING                                            ~2, [ 's':->16, 'm':->18, 'h':->20, 'd':->22, 'w':->24, 'mon':->26, ], ->28
   42     3    >   CASE                                                     ~2, 's'
          4      > JMPNZ                                                    ~3, ->16
   43     5    >   CASE                                                     ~2, 'm'
          6      > JMPNZ                                                    ~3, ->18
   44     7    >   CASE                                                     ~2, 'h'
          8      > JMPNZ                                                    ~3, ->20
   45     9    >   CASE                                                     ~2, 'd'
         10      > JMPNZ                                                    ~3, ->22
   46    11    >   CASE                                                     ~2, 'w'
         12      > JMPNZ                                                    ~3, ->24
   47    13    >   CASE                                                     ~2, 'mon'
         14      > JMPNZ                                                    ~3, ->26
         15    > > JMP                                                      ->28
   42    16    >   ASSIGN                                                   !1, 'sec'
         17      > JMP                                                      ->28
   43    18    >   ASSIGN                                                   !1, 'min'
         19      > JMP                                                      ->28
   44    20    >   ASSIGN                                                   !1, 'hour'
         21      > JMP                                                      ->28
   45    22    >   ASSIGN                                                   !1, 'day'
         23      > JMP                                                      ->28
   46    24    >   ASSIGN                                                   !1, 'week'
         25      > JMP                                                      ->28
   47    26    >   ASSIGN                                                   !1, 'month'
         27      > JMP                                                      ->28
         28    >   FREE                                                     ~2
   49    29      > RETURN                                                   !1
   50    30*     > RETURN                                                   null

End of Dynamic Function 0

End of function normalizetimespec

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/6Ftpb
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
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
   64     1        ASSIGN                                                   !1, <array>
   66     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'
   92     3      > FE_RESET_R                                       $7      !0, ->17
          4    > > FE_FETCH_R                                               $7, !3, ->17
   94     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
   95    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
   92    16    > > JMP                                                      ->4
         17    >   FE_FREE                                                  $7
   99    18      > RETURN                                                   !1
  100    19*     > RETURN                                                   null

End of function testtimespec

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.79 ms | 1017 KiB | 19 Q