3v4l.org

run code in 300+ PHP versions simultaneously
<?php class SlightlyLessBollocksMySQLi { public function prepareWithParams($query, $params) { $tokens = $this->getQueryTokens($query); $query = ''; $args = ['']; $paramRefs = []; $i = 1; foreach ($tokens as $token) { if ($token !== '?') { $query .= $token; continue; } $paramRefs[$i] = array_shift($params); switch (true) { case is_array($paramRefs[$i]): $query .= implode(', ', array_fill(0, count($paramRefs[$i]), '?')); array_splice($paramRefs, $i, 1, $paramRefs[$i]); for ($j = $i; isset($paramRefs[$j]); $j++) { switch (true) { case is_int($paramRefs[$j]): case is_bool($paramRefs[$j]): $args[0] .= 'i'; $paramRefs[$j] = (int)$paramRefs[$j]; break; case is_float($paramRefs[$j]): $args[0] .= 'd'; break; default: $args[0] .= 's'; $paramRefs[$j] = (string)$paramRefs[$j]; break; } $args[] = &$paramRefs[$j]; } $i = $j; break; case is_int($paramRefs[$i]): case is_bool($paramRefs[$i]): $args[0] .= 'i'; $query .= '?'; $paramRefs[$i] = (int)$paramRefs[$i]; $args[] = &$paramRefs[$i++]; break; case is_float($paramRefs[$i]): $args[0] .= 'd'; $query .= '?'; $args[] = &$paramRefs[$i++]; break; default: $args[0] .= 's'; $query .= '?'; $paramRefs[$i] = (string)$paramRefs[$i]; $args[] = &$paramRefs[$i++]; break; } } var_dump($query, $args); } /** * @param string $token * @return bool */ private function isQuotedString($token) { return in_array($token[0], array('"', "'")) && $token[0] == $token[strlen($token) - 1]; } /** * "Tokenize" the query * * Actually all this does is split the query up into things that are and are not quoted strings, to avoid treating * a literal question mark as a placeholder. * * @param string $query * @return string[] */ private function getQueryTokens($query) { static $expr = '/("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')/'; $tokens = []; foreach (preg_split($expr, $query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $token) { if ($this->isQuotedString($token)) { $tokens[] = $token; } else { $tokens = array_merge($tokens, preg_split('/(\?)/', $token, -1, PREG_SPLIT_DELIM_CAPTURE)); } } return $tokens; } } (new SlightlyLessBollocksMySQLi)->prepareWithParams("SELECT * FROM foo WHERE 'a' = b AND foo = ? AND bar IN(?)", [2, [1, 2, 12.345, "hello", true]]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GtaNl
function name:  (null)
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  110     0  E >   NEW                                              $0      'SlightlyLessBollocksMySQLi'
          1        DO_FCALL                                      0          
          2        INIT_METHOD_CALL                                         $0, 'prepareWithParams'
          3        SEND_VAL_EX                                              'SELECT+%2A+FROM+foo+WHERE+%27a%27+%3D+b+AND+foo+%3D+%3F+AND+bar+IN%28%3F%29'
          4        SEND_VAL_EX                                              <array>
          5        DO_FCALL                                      0          
          6      > RETURN                                                   1

Class SlightlyLessBollocksMySQLi:
Function preparewithparams:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 127
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 127
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 34
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 91
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 91
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 104
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 113
Branch analysis from position: 113
1 jumps found. (Code = 42) Position 1 = 126
Branch analysis from position: 126
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 104
1 jumps found. (Code = 42) Position 1 = 126
Branch analysis from position: 126
Branch analysis from position: 91
1 jumps found. (Code = 42) Position 1 = 126
Branch analysis from position: 126
Branch analysis from position: 91
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
2 jumps found. (Code = 44) Position 1 = 89, Position 2 = 55
Branch analysis from position: 89
1 jumps found. (Code = 42) Position 1 = 126
Branch analysis from position: 126
Branch analysis from position: 55
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 65
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 61, Position 2 = 65
Branch analysis from position: 61
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 72
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 82
Branch analysis from position: 82
2 jumps found. (Code = 44) Position 1 = 89, Position 2 = 55
Branch analysis from position: 89
Branch analysis from position: 55
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 82
Branch analysis from position: 82
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 82
Branch analysis from position: 82
Branch analysis from position: 65
Branch analysis from position: 127
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 127
filename:       /in/GtaNl
function name:  prepareWithParams
number of ops:  133
compiled vars:  !0 = $query, !1 = $params, !2 = $tokens, !3 = $args, !4 = $paramRefs, !5 = $i, !6 = $token, !7 = $j
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2        INIT_METHOD_CALL                                         'getQueryTokens'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $8      
          5        ASSIGN                                                   !2, $8
    8     6        ASSIGN                                                   !0, ''
    9     7        ASSIGN                                                   !3, <array>
   10     8        ASSIGN                                                   !4, <array>
   11     9        ASSIGN                                                   !5, 1
   13    10      > FE_RESET_R                                       $14     !2, ->127
         11    > > FE_FETCH_R                                               $14, !6, ->127
   14    12    >   IS_NOT_IDENTICAL                                         !6, '%3F'
         13      > JMPZ                                                     ~15, ->16
   15    14    >   ASSIGN_OP                                     8          !0, !6
   16    15      > JMP                                                      ->11
   19    16    >   INIT_FCALL                                               'array_shift'
         17        SEND_REF                                                 !1
         18        DO_ICALL                                         $18     
         19        ASSIGN_DIM                                               !4, !5
         20        OP_DATA                                                  $18
   21    21        FETCH_DIM_R                                      ~20     !4, !5
         22        TYPE_CHECK                                  128          ~20
         23      > JMPNZ                                                    ~21, ->34
   49    24    >   FETCH_DIM_R                                      ~22     !4, !5
         25        TYPE_CHECK                                   16          ~22
         26      > JMPNZ                                                    ~23, ->91
   50    27    >   FETCH_DIM_R                                      ~24     !4, !5
         28        TYPE_CHECK                                   12          ~24
         29      > JMPNZ                                                    ~25, ->91
   57    30    >   FETCH_DIM_R                                      ~26     !4, !5
         31        TYPE_CHECK                                   32          ~26
         32      > JMPNZ                                                    ~27, ->104
         33    > > JMP                                                      ->113
   22    34    >   INIT_FCALL                                               'implode'
         35        SEND_VAL                                                 '%2C+'
         36        INIT_FCALL                                               'array_fill'
         37        SEND_VAL                                                 0
         38        FETCH_DIM_R                                      ~28     !4, !5
         39        COUNT                                            ~29     ~28
         40        SEND_VAL                                                 ~29
         41        SEND_VAL                                                 '%3F'
         42        DO_ICALL                                         $30     
         43        SEND_VAR                                                 $30
         44        DO_ICALL                                         $31     
         45        ASSIGN_OP                                     8          !0, $31
   24    46        INIT_FCALL                                               'array_splice'
         47        SEND_REF                                                 !4
         48        SEND_VAR                                                 !5
         49        SEND_VAL                                                 1
         50        FETCH_DIM_R                                      ~33     !4, !5
         51        SEND_VAL                                                 ~33
         52        DO_ICALL                                                 
   26    53        ASSIGN                                                   !7, !5
         54      > JMP                                                      ->87
   28    55    >   FETCH_DIM_R                                      ~37     !4, !7
         56        TYPE_CHECK                                   16          ~37
         57      > JMPNZ                                                    ~38, ->65
   29    58    >   FETCH_DIM_R                                      ~39     !4, !7
         59        TYPE_CHECK                                   12          ~39
         60      > JMPNZ                                                    ~40, ->65
   34    61    >   FETCH_DIM_R                                      ~41     !4, !7
         62        TYPE_CHECK                                   32          ~41
         63      > JMPNZ                                                    ~42, ->72
         64    > > JMP                                                      ->75
   30    65    >   ASSIGN_DIM_OP                .=               8          !3, 0
         66        OP_DATA                                                  'i'
   31    67        FETCH_DIM_R                                      ~45     !4, !7
         68        CAST                                          4  ~46     ~45
         69        ASSIGN_DIM                                               !4, !7
         70        OP_DATA                                                  ~46
   32    71      > JMP                                                      ->82
   35    72    >   ASSIGN_DIM_OP                .=               8          !3, 0
         73        OP_DATA                                                  'd'
   36    74      > JMP                                                      ->82
   39    75    >   ASSIGN_DIM_OP                .=               8          !3, 0
         76        OP_DATA                                                  's'
   40    77        FETCH_DIM_R                                      ~50     !4, !7
         78        CAST                                          6  ~51     ~50
         79        ASSIGN_DIM                                               !4, !7
         80        OP_DATA                                                  ~51
   41    81      > JMP                                                      ->82
   43    82    >   FETCH_DIM_W                                      $53     !4, !7
         83        MAKE_REF                                         $54     $53
         84        FETCH_DIM_W                                      $52     !3
         85        ASSIGN_REF                                               $52, $54
   26    86        PRE_INC                                                  !7
         87    >   ISSET_ISEMPTY_DIM_OBJ                         0          !4, !7
         88      > JMPNZ                                                    ~57, ->55
   46    89    >   ASSIGN                                                   !5, !7
   47    90      > JMP                                                      ->126
   51    91    >   ASSIGN_DIM_OP                .=               8          !3, 0
         92        OP_DATA                                                  'i'
   52    93        ASSIGN_OP                                     8          !0, '%3F'
   53    94        FETCH_DIM_R                                      ~62     !4, !5
         95        CAST                                          4  ~63     ~62
         96        ASSIGN_DIM                                               !4, !5
         97        OP_DATA                                                  ~63
   54    98        POST_INC                                         ~65     !5
         99        FETCH_DIM_W                                      $66     !4, ~65
        100        MAKE_REF                                         $67     $66
        101        FETCH_DIM_W                                      $64     !3
        102        ASSIGN_REF                                               $64, $67
   55   103      > JMP                                                      ->126
   58   104    >   ASSIGN_DIM_OP                .=               8          !3, 0
        105        OP_DATA                                                  'd'
   59   106        ASSIGN_OP                                     8          !0, '%3F'
   60   107        POST_INC                                         ~72     !5
        108        FETCH_DIM_W                                      $73     !4, ~72
        109        MAKE_REF                                         $74     $73
        110        FETCH_DIM_W                                      $71     !3
        111        ASSIGN_REF                                               $71, $74
   61   112      > JMP                                                      ->126
   64   113    >   ASSIGN_DIM_OP                .=               8          !3, 0
        114        OP_DATA                                                  's'
   65   115        ASSIGN_OP                                     8          !0, '%3F'
   66   116        FETCH_DIM_R                                      ~79     !4, !5
        117        CAST                                          6  ~80     ~79
        118        ASSIGN_DIM                                               !4, !5
        119        OP_DATA                                                  ~80
   67   120        POST_INC                                         ~82     !5
        121        FETCH_DIM_W                                      $83     !4, ~82
        122        MAKE_REF                                         $84     $83
        123        FETCH_DIM_W                                      $81     !3
        124        ASSIGN_REF                                               $81, $84
   68   125      > JMP                                                      ->126
   13   126    > > JMP                                                      ->11
        127    >   FE_FREE                                                  $14
   72   128        INIT_FCALL                                               'var_dump'
        129        SEND_VAR                                                 !0
        130        SEND_VAR                                                 !3
        131        DO_ICALL                                                 
   73   132      > RETURN                                                   null

End of function preparewithparams

Function isquotedstring:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/GtaNl
function name:  isQuotedString
number of ops:  12
compiled vars:  !0 = $token
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   RECV                                             !0      
   81     1        FETCH_DIM_R                                      ~1      !0, 0
          2        IN_ARRAY                                         ~2      ~1, <array>
          3      > JMPZ_EX                                          ~2      ~2, ->10
          4    >   FETCH_DIM_R                                      ~3      !0, 0
          5        STRLEN                                           ~4      !0
          6        SUB                                              ~5      ~4, 1
          7        FETCH_DIM_R                                      ~6      !0, ~5
          8        IS_EQUAL                                         ~7      ~3, ~6
          9        BOOL                                             ~2      ~7
         10    > > RETURN                                                   ~2
   82    11*     > RETURN                                                   null

End of function isquotedstring

Function getquerytokens:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 30
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 30
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/GtaNl
function name:  getQueryTokens
number of ops:  33
compiled vars:  !0 = $query, !1 = $expr, !2 = $tokens, !3 = $token
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   93     0  E >   RECV                                             !0      
   95     1        BIND_STATIC                                              !1
   96     2        ASSIGN                                                   !2, <array>
   98     3        INIT_FCALL                                               'preg_split'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 -1
          7        SEND_VAL                                                 3
          8        DO_ICALL                                         $5      
          9      > FE_RESET_R                                       $6      $5, ->30
         10    > > FE_FETCH_R                                               $6, !3, ->30
   99    11    >   INIT_METHOD_CALL                                         'isQuotedString'
         12        SEND_VAR                                                 !3
         13        DO_FCALL                                      0  $7      
         14      > JMPZ                                                     $7, ->18
  100    15    >   ASSIGN_DIM                                               !2
         16        OP_DATA                                                  !3
         17      > JMP                                                      ->29
  102    18    >   INIT_FCALL                                               'array_merge'
         19        SEND_VAR                                                 !2
         20        INIT_FCALL                                               'preg_split'
         21        SEND_VAL                                                 '%2F%28%5C%3F%29%2F'
         22        SEND_VAR                                                 !3
         23        SEND_VAL                                                 -1
         24        SEND_VAL                                                 2
         25        DO_ICALL                                         $9      
         26        SEND_VAR                                                 $9
         27        DO_ICALL                                         $10     
         28        ASSIGN                                                   !2, $10
   98    29    > > JMP                                                      ->10
         30    >   FE_FREE                                                  $6
  106    31      > RETURN                                                   !2
  107    32*     > RETURN                                                   null

End of function getquerytokens

End of class SlightlyLessBollocksMySQLi.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.2 ms | 1404 KiB | 27 Q