3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Tokenizes text that looks something like SQL. */ function tokenizeSQL( $SQL ) { $functions = array ( 'concat', 'if' ); $token = '\\(|\\)|[\']|"|\140|[*]|,|<|>|<>|=|[+]'; $terminal = $token.'|;| |\\n'; $result = array(); $string = $SQL; $string = ltrim($string); $string = rtrim($string,';').';'; // always ends with a terminal $string = preg_replace( "/[\n\r]/s", ' ', $string ); while( preg_match( "/^($token)($terminal)/s", $string, $matches ) || preg_match( "/^({$token})./s", $string, $matches ) || preg_match( "/^([a-zA-Z0-9_.]+?)($terminal)/s", $string, $matches) ) { $t = $matches[1]; if ($t=='\'') { // it's a string $t = tokSingleQuoteString( $string ); array_push($result, $t); } else if ($t=="\140") { // it's a backtick string (a name) $t = tokBackQuoteString( $string ); array_push($result, $t); } else if ($t=='"') { // it's a double quoted string (a name in normal sql) $t = tokDoubleQuoteString( $string ); array_push($result, $t); } else { array_push($result, $t); } $string = substr( $string, strlen($t) ); $string = ltrim($string); } return $result; } function tokSingleQuoteString( $string ) { // matches a single-quoted string in $string // $string starts with a single quote preg_match('/^(\'.*?\').*$/s', $string, $matches ); return $matches[1]; } function tokBackQuoteString( $string ) { // matches a back-quoted string in $string // $string starts with a back quote preg_match('/^([\140].*?[\140]).*$/s', $string, $matches ); return $matches[1]; } function tokDoubleQuoteString( $string ) { // matches a back-quoted string in $string // $string starts with a back quote preg_match('/^(".*?").*$/s', $string, $matches ); return $matches[1]; } print "<pre>"; print_r(tokenizeSQL('SELECT name, rew from asdf where id in(select papa from sdfff')); print "</pre>";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bJYGe
function name:  (null)
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   ECHO                                                     '%3Cpre%3E'
   77     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'tokenizesql'
          3        SEND_VAL                                                 'SELECT+name%2C+rew+from+asdf+where+id+in%28select+papa++from+sdfff'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
   78     7        ECHO                                                     '%3C%2Fpre%3E'
          8      > RETURN                                                   1

Function tokenizesql:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
2 jumps found. (Code = 47) Position 1 = 84, Position 2 = 93
Branch analysis from position: 84
2 jumps found. (Code = 47) Position 1 = 94, Position 2 = 103
Branch analysis from position: 94
2 jumps found. (Code = 44) Position 1 = 104, Position 2 = 24
Branch analysis from position: 104
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 37
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
2 jumps found. (Code = 47) Position 1 = 84, Position 2 = 93
Branch analysis from position: 84
Branch analysis from position: 93
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 48
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 59
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
Branch analysis from position: 59
2 jumps found. (Code = 47) Position 1 = 84, Position 2 = 93
Branch analysis from position: 84
Branch analysis from position: 93
Branch analysis from position: 103
Branch analysis from position: 93
filename:       /in/bJYGe
function name:  tokenizeSQL
number of ops:  106
compiled vars:  !0 = $SQL, !1 = $functions, !2 = $token, !3 = $terminal, !4 = $result, !5 = $string, !6 = $t, !7 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
    8     1        ASSIGN                                                   !1, <array>
    9     2        ASSIGN                                                   !2, '%5C%28%7C%5C%29%7C%5B%27%5D%7C%22%7C%5C140%7C%5B%2A%5D%7C%2C%7C%3C%7C%3E%7C%3C%3E%7C%3D%7C%5B%2B%5D'
   10     3        CONCAT                                           ~10     !2, '%7C%3B%7C+%7C%5Cn'
          4        ASSIGN                                                   !3, ~10
   11     5        ASSIGN                                                   !4, <array>
   12     6        ASSIGN                                                   !5, !0
   13     7        INIT_FCALL                                               'ltrim'
          8        SEND_VAR                                                 !5
          9        DO_ICALL                                         $14     
         10        ASSIGN                                                   !5, $14
   14    11        INIT_FCALL                                               'rtrim'
         12        SEND_VAR                                                 !5
         13        SEND_VAL                                                 '%3B'
         14        DO_ICALL                                         $16     
         15        CONCAT                                           ~17     $16, '%3B'
         16        ASSIGN                                                   !5, ~17
   15    17        INIT_FCALL                                               'preg_replace'
         18        SEND_VAL                                                 '%2F%5B%0A%0D%5D%2Fs'
         19        SEND_VAL                                                 '+'
         20        SEND_VAR                                                 !5
         21        DO_ICALL                                         $19     
         22        ASSIGN                                                   !5, $19
   17    23      > JMP                                                      ->73
   22    24    >   FETCH_DIM_R                                      ~21     !7, 1
         25        ASSIGN                                                   !6, ~21
   23    26        IS_EQUAL                                                 !6, '%27'
         27      > JMPZ                                                     ~23, ->37
   26    28    >   INIT_FCALL_BY_NAME                                       'tokSingleQuoteString'
         29        SEND_VAR_EX                                              !5
         30        DO_FCALL                                      0  $24     
         31        ASSIGN                                                   !6, $24
   27    32        INIT_FCALL                                               'array_push'
         33        SEND_REF                                                 !4
         34        SEND_VAR                                                 !6
         35        DO_ICALL                                                 
         36      > JMP                                                      ->63
   29    37    >   IS_EQUAL                                                 !6, '%60'
         38      > JMPZ                                                     ~27, ->48
   32    39    >   INIT_FCALL_BY_NAME                                       'tokBackQuoteString'
         40        SEND_VAR_EX                                              !5
         41        DO_FCALL                                      0  $28     
         42        ASSIGN                                                   !6, $28
   33    43        INIT_FCALL                                               'array_push'
         44        SEND_REF                                                 !4
         45        SEND_VAR                                                 !6
         46        DO_ICALL                                                 
         47      > JMP                                                      ->63
   35    48    >   IS_EQUAL                                                 !6, '%22'
         49      > JMPZ                                                     ~31, ->59
   38    50    >   INIT_FCALL_BY_NAME                                       'tokDoubleQuoteString'
         51        SEND_VAR_EX                                              !5
         52        DO_FCALL                                      0  $32     
         53        ASSIGN                                                   !6, $32
   39    54        INIT_FCALL                                               'array_push'
         55        SEND_REF                                                 !4
         56        SEND_VAR                                                 !6
         57        DO_ICALL                                                 
         58      > JMP                                                      ->63
   43    59    >   INIT_FCALL                                               'array_push'
         60        SEND_REF                                                 !4
         61        SEND_VAR                                                 !6
         62        DO_ICALL                                                 
   45    63    >   INIT_FCALL                                               'substr'
         64        SEND_VAR                                                 !5
         65        STRLEN                                           ~36     !6
         66        SEND_VAL                                                 ~36
         67        DO_ICALL                                         $37     
         68        ASSIGN                                                   !5, $37
   46    69        INIT_FCALL                                               'ltrim'
         70        SEND_VAR                                                 !5
         71        DO_ICALL                                         $39     
         72        ASSIGN                                                   !5, $39
   17    73    >   INIT_FCALL                                               'preg_match'
         74        ROPE_INIT                                     5  ~42     '%2F%5E%28'
         75        ROPE_ADD                                      1  ~42     ~42, !2
         76        ROPE_ADD                                      2  ~42     ~42, '%29%28'
         77        ROPE_ADD                                      3  ~42     ~42, !3
         78        ROPE_END                                      4  ~41     ~42, '%29%2Fs'
         79        SEND_VAL                                                 ~41
         80        SEND_VAR                                                 !5
         81        SEND_REF                                                 !7
         82        DO_ICALL                                         $45     
         83      > JMPNZ_EX                                         ~46     $45, ->93
   18    84    >   INIT_FCALL                                               'preg_match'
         85        ROPE_INIT                                     3  ~48     '%2F%5E%28'
         86        ROPE_ADD                                      1  ~48     ~48, !2
         87        ROPE_END                                      2  ~47     ~48, '%29.%2Fs'
         88        SEND_VAL                                                 ~47
         89        SEND_VAR                                                 !5
         90        SEND_REF                                                 !7
         91        DO_ICALL                                         $50     
         92        BOOL                                             ~46     $50
         93    > > JMPNZ_EX                                         ~46     ~46, ->103
   19    94    >   INIT_FCALL                                               'preg_match'
         95        ROPE_INIT                                     3  ~52     '%2F%5E%28%5Ba-zA-Z0-9_.%5D%2B%3F%29%28'
         96        ROPE_ADD                                      1  ~52     ~52, !3
         97        ROPE_END                                      2  ~51     ~52, '%29%2Fs'
         98        SEND_VAL                                                 ~51
         99        SEND_VAR                                                 !5
        100        SEND_REF                                                 !7
        101        DO_ICALL                                         $54     
        102        BOOL                                             ~46     $54
        103    > > JMPNZ                                                    ~46, ->24
   48   104    > > RETURN                                                   !4
   49   105*     > RETURN                                                   null

End of function tokenizesql

Function toksinglequotestring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bJYGe
function name:  tokSingleQuoteString
number of ops:  9
compiled vars:  !0 = $string, !1 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
   55     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%2F%5E%28%27.%2A%3F%27%29.%2A%24%2Fs'
          3        SEND_VAR                                                 !0
          4        SEND_REF                                                 !1
          5        DO_ICALL                                                 
   56     6        FETCH_DIM_R                                      ~3      !1, 1
          7      > RETURN                                                   ~3
   57     8*     > RETURN                                                   null

End of function toksinglequotestring

Function tokbackquotestring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bJYGe
function name:  tokBackQuoteString
number of ops:  9
compiled vars:  !0 = $string, !1 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   63     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%2F%5E%28%5B%5C140%5D.%2A%3F%5B%5C140%5D%29.%2A%24%2Fs'
          3        SEND_VAR                                                 !0
          4        SEND_REF                                                 !1
          5        DO_ICALL                                                 
   64     6        FETCH_DIM_R                                      ~3      !1, 1
          7      > RETURN                                                   ~3
   65     8*     > RETURN                                                   null

End of function tokbackquotestring

Function tokdoublequotestring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bJYGe
function name:  tokDoubleQuoteString
number of ops:  9
compiled vars:  !0 = $string, !1 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   RECV                                             !0      
   71     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%2F%5E%28%22.%2A%3F%22%29.%2A%24%2Fs'
          3        SEND_VAR                                                 !0
          4        SEND_REF                                                 !1
          5        DO_ICALL                                                 
   72     6        FETCH_DIM_R                                      ~3      !1, 1
          7      > RETURN                                                   ~3
   73     8*     > RETURN                                                   null

End of function tokdoublequotestring

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.65 ms | 1411 KiB | 28 Q