3v4l.org

run code in 300+ PHP versions simultaneously
<?php $filenameNew = 'PX20170713_103320_2651093_00001'; $filenameOld = '5956e5781e4f1-20170630_233347_5712837_00001.xml'; //$timeslug = implode(array_slice(explode('_', rtrim(basename($fileToProcess), ".xml" )), 0, 2)); //$datetime = date('c', strtotime($timeslug)); /** * Get a date time object from an AX filename. * * Example filename: PX20170713_103320_2651093_00001.xml */ function getDateFromFilename(string $filename): ?DateTime { // Find the first block of 4-2-2 numbers. $dateRegexp = '/([0-9]{4})([0-9]{2})([0-9]{2})/'; // Find the first block of 2-2-2 numbers $timeRegexp = '/([0-9]{2})([0-9]{2})([0-9]{2})/'; // We could do the regex directly to the filename but by extracting // the date and time from the filename string we retain a level of // safety in that we can not accidentally regexp the wrong value. try { list($date, $time) = explode('_', $filename); } catch (\Throwable $t) { throw new \Exception(sprintf( 'Could not split the date up from the filename for %s', $filename )); } // Regex the date and drop the first array index(the entire string block) try { preg_match($dateRegexp, $date, $dateParts); unset($dateParts[0]); } catch (\Throwable $t) { throw new \Exception(sprintf( 'Could not extrapolate the date from the filename %s', $filename )); } // Regex the time try { preg_match($timeRegexp, $time, $timeParts); unset($timeParts[0]); } catch (\Throwable $t) { throw new \Exception(sprintf( 'Could not extrapolate the time from the filename %s', $filename )); } // Create a new datetime instance and set the date from the regex results. try { $dateTime = new \DateTime(); $dateTime->setDate($dateParts[1], $dateParts[2], $dateParts[3]); $dateTime->setTime($timeParts[1], $timeParts[2], $timeParts[3]); } catch (\Throwable $t) { throw new \Exception(sprintf( 'Could not build the date time from the filename %s', $filename )); return null; } return $dateTime; } var_dump(getDateFromFilename($filenameNew));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ojiEa
function name:  (null)
number of ops:  9
compiled vars:  !0 = $filenameNew, !1 = $filenameOld
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'PX20170713_103320_2651093_00001'
    4     1        ASSIGN                                                   !1, '5956e5781e4f1-20170630_233347_5712837_00001.xml'
   73     2        INIT_FCALL                                               'var_dump'
          3        INIT_FCALL                                               'getdatefromfilename'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $4      
          6        SEND_VAR                                                 $4
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

Function getdatefromfilename:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 90
Branch analysis from position: 90
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 13
Branch analysis from position: 13
2 jumps found. (Code = 107) Position 1 = 14, Position 2 = -2
Branch analysis from position: 14
1 jumps found. (Code = 108) Position 1 = -2
Found catch point at position: 29
Branch analysis from position: 29
2 jumps found. (Code = 107) Position 1 = 30, Position 2 = -2
Branch analysis from position: 30
1 jumps found. (Code = 108) Position 1 = -2
Found catch point at position: 45
Branch analysis from position: 45
2 jumps found. (Code = 107) Position 1 = 46, Position 2 = -2
Branch analysis from position: 46
1 jumps found. (Code = 108) Position 1 = -2
Found catch point at position: 80
Branch analysis from position: 80
2 jumps found. (Code = 107) Position 1 = 81, Position 2 = -2
Branch analysis from position: 81
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/ojiEa
function name:  getDateFromFilename
number of ops:  94
compiled vars:  !0 = $filename, !1 = $dateRegexp, !2 = $timeRegexp, !3 = $date, !4 = $time, !5 = $t, !6 = $dateParts, !7 = $timeParts, !8 = $dateTime
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   17     1        ASSIGN                                                   !1, '%2F%28%5B0-9%5D%7B4%7D%29%28%5B0-9%5D%7B2%7D%29%28%5B0-9%5D%7B2%7D%29%2F'
   20     2        ASSIGN                                                   !2, '%2F%28%5B0-9%5D%7B2%7D%29%28%5B0-9%5D%7B2%7D%29%28%5B0-9%5D%7B2%7D%29%2F'
   26     3        INIT_FCALL                                               'explode'
          4        SEND_VAL                                                 '_'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $11     
          7        FETCH_LIST_R                                     $12     $11, 0
          8        ASSIGN                                                   !3, $12
          9        FETCH_LIST_R                                     $14     $11, 1
         10        ASSIGN                                                   !4, $14
         11        FREE                                                     $11
         12      > JMP                                                      ->22
   27    13  E > > CATCH                                       last         'Throwable'
   28    14    >   NEW                                              $16     'Exception'
         15        INIT_FCALL                                               'sprintf'
   29    16        SEND_VAL                                                 'Could+not+split+the+date+up+from+the+filename+for+%25s'
   30    17        SEND_VAR                                                 !0
         18        DO_ICALL                                         $17     
         19        SEND_VAR_NO_REF_EX                                       $17
         20        DO_FCALL                                      0          
         21      > THROW                                         0          $16
   36    22    >   INIT_FCALL                                               'preg_match'
         23        SEND_VAR                                                 !1
         24        SEND_VAR                                                 !3
         25        SEND_REF                                                 !6
         26        DO_ICALL                                                 
   37    27        UNSET_DIM                                                !6, 0
         28      > JMP                                                      ->38
   38    29  E > > CATCH                                       last         'Throwable'
   39    30    >   NEW                                              $20     'Exception'
         31        INIT_FCALL                                               'sprintf'
   40    32        SEND_VAL                                                 'Could+not+extrapolate+the+date+from+the+filename+%25s'
   41    33        SEND_VAR                                                 !0
         34        DO_ICALL                                         $21     
         35        SEND_VAR_NO_REF_EX                                       $21
         36        DO_FCALL                                      0          
         37      > THROW                                         0          $20
   47    38    >   INIT_FCALL                                               'preg_match'
         39        SEND_VAR                                                 !2
         40        SEND_VAR                                                 !4
         41        SEND_REF                                                 !7
         42        DO_ICALL                                                 
   48    43        UNSET_DIM                                                !7, 0
         44      > JMP                                                      ->54
   49    45  E > > CATCH                                       last         'Throwable'
   50    46    >   NEW                                              $24     'Exception'
         47        INIT_FCALL                                               'sprintf'
   51    48        SEND_VAL                                                 'Could+not+extrapolate+the+time+from+the+filename+%25s'
   52    49        SEND_VAR                                                 !0
         50        DO_ICALL                                         $25     
         51        SEND_VAR_NO_REF_EX                                       $25
         52        DO_FCALL                                      0          
         53      > THROW                                         0          $24
   58    54    >   NEW                                              $27     'DateTime'
         55        DO_FCALL                                      0          
         56        ASSIGN                                                   !8, $27
   59    57        INIT_METHOD_CALL                                         !8, 'setDate'
         58        CHECK_FUNC_ARG                                           
         59        FETCH_DIM_FUNC_ARG                               $30     !6, 1
         60        SEND_FUNC_ARG                                            $30
         61        CHECK_FUNC_ARG                                           
         62        FETCH_DIM_FUNC_ARG                               $31     !6, 2
         63        SEND_FUNC_ARG                                            $31
         64        CHECK_FUNC_ARG                                           
         65        FETCH_DIM_FUNC_ARG                               $32     !6, 3
         66        SEND_FUNC_ARG                                            $32
         67        DO_FCALL                                      0          
   60    68        INIT_METHOD_CALL                                         !8, 'setTime'
         69        CHECK_FUNC_ARG                                           
         70        FETCH_DIM_FUNC_ARG                               $34     !7, 1
         71        SEND_FUNC_ARG                                            $34
         72        CHECK_FUNC_ARG                                           
         73        FETCH_DIM_FUNC_ARG                               $35     !7, 2
         74        SEND_FUNC_ARG                                            $35
         75        CHECK_FUNC_ARG                                           
         76        FETCH_DIM_FUNC_ARG                               $36     !7, 3
         77        SEND_FUNC_ARG                                            $36
         78        DO_FCALL                                      0          
         79      > JMP                                                      ->90
   61    80  E > > CATCH                                       last         'Throwable'
   62    81    >   NEW                                              $38     'Exception'
         82        INIT_FCALL                                               'sprintf'
   63    83        SEND_VAL                                                 'Could+not+build+the+date+time+from+the+filename+%25s'
   64    84        SEND_VAR                                                 !0
         85        DO_ICALL                                         $39     
         86        SEND_VAR_NO_REF_EX                                       $39
         87        DO_FCALL                                      0          
         88      > THROW                                         0          $38
   67    89*       RETURN                                                   null
   70    90    >   VERIFY_RETURN_TYPE                                       !8
         91      > RETURN                                                   !8
   71    92*       VERIFY_RETURN_TYPE                                       
         93*     > RETURN                                                   null

End of function getdatefromfilename

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.15 ms | 1407 KiB | 22 Q