3v4l.org

run code in 300+ PHP versions simultaneously
<?php function base62encode($data) { $outstring = ''; $l = strlen($data); for ($i = 0; $i < $l; $i += 8) { $chunk = substr($data, $i, 8); $outlen = ceil((strlen($chunk) * 8)/6); //8bit/char in, 6bits/char out, round up $x = bin2hex($chunk); //gmp won't convert from binary, so go via hex $w = gmp_strval(gmp_init(ltrim($x, '0'), 16), 62); //gmp doesn't like leading 0s $pad = str_pad($w, $outlen, '0', STR_PAD_LEFT); $outstring .= $pad; } return $outstring; } /** * Decode base-62 encoded text into binary * Note that because base-62 encodes slightly less than 6 bits per character (actually 5.95419631038688), there is some wastage * In order to make this practical, we chunk in groups of up to 11 input chars, which give up to 8 output chars * with a wastage of up to 4 bits per chunk, so while the output is not quite as space efficient as a * true multiprecision conversion, it's orders of magnitude faster * Note that the input of this function is not compatible with that of a multiprecision conversion, but it's a practical encoding implementation * The encoding overhead tends towards 37.5% with this chunk size; bigger chunk sizes can be slightly more space efficient, but may be slower * Base-64 doesn't suffer this problem because it fits into exactly 6 bits, so it generates the same results as a multiprecision conversion * Requires PHP 5.3.2 and gmp 4.2.0 * @param string $data Base-62 encoded text (not chunked or split) * @return string Decoded binary data */ function base62decode($data) { $outstring = ''; $l = strlen($data); for ($i = 0; $i < $l; $i += 11) { $chunk = substr($data, $i, 11); $outlen = floor((strlen($chunk) * 6)/8); //6bit/char in, 8bits/char out, round down $y = gmp_strval(gmp_init(ltrim($chunk, '0'), 62), 16); //gmp doesn't like leading 0s $pad = str_pad($y, $outlen * 2, '0', STR_PAD_LEFT); //double output length as as we're going via hex (4bits/char) $outstring .= pack('H*', $pad); //same as hex2bin } return $outstring; } $data = "?1,2,3,45,4536,1234567"; $encode = base62encode($data); echo 'Encode: '.$encode; echo ' - '.base62decode($encode);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MtBjQ
function name:  (null)
number of ops:  13
compiled vars:  !0 = $data, !1 = $encode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   ASSIGN                                                   !0, '%3F1%2C2%2C3%2C45%2C4536%2C1234567'
   43     1        INIT_FCALL                                               'base62encode'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
   44     5        CONCAT                                           ~5      'Encode%3A+', !1
          6        ECHO                                                     ~5
   45     7        INIT_FCALL                                               'base62decode'
          8        SEND_VAR                                                 !1
          9        DO_FCALL                                      0  $6      
         10        CONCAT                                           ~7      '+-+', $6
         11        ECHO                                                     ~7
         12      > RETURN                                                   1

Function base62encode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 6
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 6
Branch analysis from position: 47
Branch analysis from position: 6
filename:       /in/MtBjQ
function name:  base62encode
number of ops:  49
compiled vars:  !0 = $data, !1 = $outstring, !2 = $l, !3 = $i, !4 = $chunk, !5 = $outlen, !6 = $x, !7 = $w, !8 = $pad
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, ''
    5     2        STRLEN                                           ~10     !0
          3        ASSIGN                                                   !2, ~10
    6     4        ASSIGN                                                   !3, 0
          5      > JMP                                                      ->45
    7     6    >   INIT_FCALL                                               'substr'
          7        SEND_VAR                                                 !0
          8        SEND_VAR                                                 !3
          9        SEND_VAL                                                 8
         10        DO_ICALL                                         $13     
         11        ASSIGN                                                   !4, $13
    8    12        INIT_FCALL                                               'ceil'
         13        STRLEN                                           ~15     !4
         14        MUL                                              ~16     ~15, 8
         15        DIV                                              ~17     ~16, 6
         16        SEND_VAL                                                 ~17
         17        DO_ICALL                                         $18     
         18        ASSIGN                                                   !5, $18
    9    19        INIT_FCALL                                               'bin2hex'
         20        SEND_VAR                                                 !4
         21        DO_ICALL                                         $20     
         22        ASSIGN                                                   !6, $20
   10    23        INIT_FCALL_BY_NAME                                       'gmp_strval'
         24        INIT_FCALL_BY_NAME                                       'gmp_init'
         25        INIT_FCALL                                               'ltrim'
         26        SEND_VAR                                                 !6
         27        SEND_VAL                                                 '0'
         28        DO_ICALL                                         $22     
         29        SEND_VAR_NO_REF_EX                                       $22
         30        SEND_VAL_EX                                              16
         31        DO_FCALL                                      0  $23     
         32        SEND_VAR_NO_REF_EX                                       $23
         33        SEND_VAL_EX                                              62
         34        DO_FCALL                                      0  $24     
         35        ASSIGN                                                   !7, $24
   11    36        INIT_FCALL                                               'str_pad'
         37        SEND_VAR                                                 !7
         38        SEND_VAR                                                 !5
         39        SEND_VAL                                                 '0'
         40        SEND_VAL                                                 0
         41        DO_ICALL                                         $26     
         42        ASSIGN                                                   !8, $26
   12    43        ASSIGN_OP                                     8          !1, !8
    6    44        ASSIGN_OP                                     1          !3, 8
         45    >   IS_SMALLER                                               !3, !2
         46      > JMPNZ                                                    ~30, ->6
   14    47    > > RETURN                                                   !1
   15    48*     > RETURN                                                   null

End of function base62encode

Function base62decode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 6
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 6
Branch analysis from position: 48
Branch analysis from position: 6
filename:       /in/MtBjQ
function name:  base62decode
number of ops:  50
compiled vars:  !0 = $data, !1 = $outstring, !2 = $l, !3 = $i, !4 = $chunk, !5 = $outlen, !6 = $y, !7 = $pad
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   30     1        ASSIGN                                                   !1, ''
   31     2        STRLEN                                           ~9      !0
          3        ASSIGN                                                   !2, ~9
   32     4        ASSIGN                                                   !3, 0
          5      > JMP                                                      ->46
   33     6    >   INIT_FCALL                                               'substr'
          7        SEND_VAR                                                 !0
          8        SEND_VAR                                                 !3
          9        SEND_VAL                                                 11
         10        DO_ICALL                                         $12     
         11        ASSIGN                                                   !4, $12
   34    12        INIT_FCALL                                               'floor'
         13        STRLEN                                           ~14     !4
         14        MUL                                              ~15     ~14, 6
         15        DIV                                              ~16     ~15, 8
         16        SEND_VAL                                                 ~16
         17        DO_ICALL                                         $17     
         18        ASSIGN                                                   !5, $17
   35    19        INIT_FCALL_BY_NAME                                       'gmp_strval'
         20        INIT_FCALL_BY_NAME                                       'gmp_init'
         21        INIT_FCALL                                               'ltrim'
         22        SEND_VAR                                                 !4
         23        SEND_VAL                                                 '0'
         24        DO_ICALL                                         $19     
         25        SEND_VAR_NO_REF_EX                                       $19
         26        SEND_VAL_EX                                              62
         27        DO_FCALL                                      0  $20     
         28        SEND_VAR_NO_REF_EX                                       $20
         29        SEND_VAL_EX                                              16
         30        DO_FCALL                                      0  $21     
         31        ASSIGN                                                   !6, $21
   36    32        INIT_FCALL                                               'str_pad'
         33        SEND_VAR                                                 !6
         34        MUL                                              ~23     !5, 2
         35        SEND_VAL                                                 ~23
         36        SEND_VAL                                                 '0'
         37        SEND_VAL                                                 0
         38        DO_ICALL                                         $24     
         39        ASSIGN                                                   !7, $24
   37    40        INIT_FCALL                                               'pack'
         41        SEND_VAL                                                 'H%2A'
         42        SEND_VAR                                                 !7
         43        DO_ICALL                                         $26     
         44        ASSIGN_OP                                     8          !1, $26
   32    45        ASSIGN_OP                                     1          !3, 11
         46    >   IS_SMALLER                                               !3, !2
         47      > JMPNZ                                                    ~29, ->6
   39    48    > > RETURN                                                   !1
   40    49*     > RETURN                                                   null

End of function base62decode

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
184.29 ms | 1407 KiB | 35 Q