3v4l.org

run code in 300+ PHP versions simultaneously
<?php function fputcsv2(&$handle, $fields = array(), $delimiter = ',', $enclosure = '"', $escape_char = '\\') { $str = ''; foreach ($fields as $value) { $needQuote = strpos($value, $delimiter) !== false || strpos($value, $enclosure) !== false || strpos($value, "\n") !== false || strpos($value, "\r") !== false || strpos($value, "\t") !== false || strpos($value, ' ') !== false; if ($needQuote) { $str2 = $enclosure; $escaped = 0; $len = strlen($value); for ($i = 0; $i < $len; $i++) { if ($value[$i] == $escape_char) { $escaped = 1; } else if (!$escaped && $value[$i] == $enclosure) { $str2 .= $enclosure; } else { $escaped = 0; } $str2 .= $value[$i]; } $str2 .= $enclosure; $str .= $str2.$delimiter; } else { $str .= $value.$delimiter; } } $str = substr($str,0,-1); $str .= "\n"; return fwrite($handle, $str); } $list = array ( array('aaa,', 'bbb', 'ccc', 'dddd'), array('123', '456', '789'), array('"aaa"', '"bbb"') ); $fp = fopen('/tmp/file.csv', 'w'); foreach ($list as $fields) { fputcsv2($fp, $fields, ',,'); } fclose($fp); echo file_get_contents('/tmp/file.csv');
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 14
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/jGNWf
function name:  (null)
number of ops:  23
compiled vars:  !0 = $list, !1 = $fp, !2 = $fields
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   ASSIGN                                                   !0, <array>
   44     1        INIT_FCALL                                               'fopen'
          2        SEND_VAL                                                 '%2Ftmp%2Ffile.csv'
          3        SEND_VAL                                                 'w'
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
   45     6      > FE_RESET_R                                       $6      !0, ->14
          7    > > FE_FETCH_R                                               $6, !2, ->14
   46     8    >   INIT_FCALL                                               'fputcsv2'
          9        SEND_REF                                                 !1
         10        SEND_VAR                                                 !2
         11        SEND_VAL                                                 '%2C%2C'
         12        DO_FCALL                                      0          
   45    13      > JMP                                                      ->7
         14    >   FE_FREE                                                  $6
   48    15        INIT_FCALL                                               'fclose'
         16        SEND_VAR                                                 !1
         17        DO_ICALL                                                 
   50    18        INIT_FCALL                                               'file_get_contents'
         19        SEND_VAL                                                 '%2Ftmp%2Ffile.csv'
         20        DO_ICALL                                         $9      
         21        ECHO                                                     $9
         22      > RETURN                                                   1

Function fputcsv2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 82
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 82
Branch analysis from position: 8
2 jumps found. (Code = 47) Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
2 jumps found. (Code = 47) Position 1 = 21, Position 2 = 27
Branch analysis from position: 21
2 jumps found. (Code = 47) Position 1 = 28, Position 2 = 34
Branch analysis from position: 28
2 jumps found. (Code = 47) Position 1 = 35, Position 2 = 41
Branch analysis from position: 35
2 jumps found. (Code = 47) Position 1 = 42, Position 2 = 48
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 79
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 56
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 61
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 56
Branch analysis from position: 75
Branch analysis from position: 56
Branch analysis from position: 61
2 jumps found. (Code = 46) Position 1 = 63, Position 2 = 66
Branch analysis from position: 63
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 69
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
Branch analysis from position: 69
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 56
Branch analysis from position: 75
Branch analysis from position: 56
Branch analysis from position: 66
Branch analysis from position: 79
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 48
Branch analysis from position: 41
Branch analysis from position: 34
Branch analysis from position: 27
Branch analysis from position: 20
Branch analysis from position: 82
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 82
filename:       /in/jGNWf
function name:  fputcsv2
number of ops:  96
compiled vars:  !0 = $handle, !1 = $fields, !2 = $delimiter, !3 = $enclosure, !4 = $escape_char, !5 = $str, !6 = $value, !7 = $needQuote, !8 = $str2, !9 = $escaped, !10 = $len, !11 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      '%2C'
          3        RECV_INIT                                        !3      '%22'
          4        RECV_INIT                                        !4      '%5C'
    4     5        ASSIGN                                                   !5, ''
    5     6      > FE_RESET_R                                       $13     !1, ->82
          7    > > FE_FETCH_R                                               $13, !6, ->82
    7     8    >   INIT_FCALL                                               'strpos'
          9        SEND_VAR                                                 !6
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                         $14     
         12        TYPE_CHECK                                  1018  ~15     $14
         13      > JMPNZ_EX                                         ~15     ~15, ->20
    8    14    >   INIT_FCALL                                               'strpos'
         15        SEND_VAR                                                 !6
         16        SEND_VAR                                                 !3
         17        DO_ICALL                                         $16     
         18        TYPE_CHECK                                  1018  ~17     $16
         19        BOOL                                             ~15     ~17
         20    > > JMPNZ_EX                                         ~15     ~15, ->27
    9    21    >   INIT_FCALL                                               'strpos'
         22        SEND_VAR                                                 !6
         23        SEND_VAL                                                 '%0A'
         24        DO_ICALL                                         $18     
         25        TYPE_CHECK                                  1018  ~19     $18
         26        BOOL                                             ~15     ~19
         27    > > JMPNZ_EX                                         ~15     ~15, ->34
   10    28    >   INIT_FCALL                                               'strpos'
         29        SEND_VAR                                                 !6
         30        SEND_VAL                                                 '%0D'
         31        DO_ICALL                                         $20     
         32        TYPE_CHECK                                  1018  ~21     $20
         33        BOOL                                             ~15     ~21
         34    > > JMPNZ_EX                                         ~15     ~15, ->41
   11    35    >   INIT_FCALL                                               'strpos'
         36        SEND_VAR                                                 !6
         37        SEND_VAL                                                 '%09'
         38        DO_ICALL                                         $22     
         39        TYPE_CHECK                                  1018  ~23     $22
         40        BOOL                                             ~15     ~23
         41    > > JMPNZ_EX                                         ~15     ~15, ->48
   12    42    >   INIT_FCALL                                               'strpos'
         43        SEND_VAR                                                 !6
         44        SEND_VAL                                                 '+'
         45        DO_ICALL                                         $24     
         46        TYPE_CHECK                                  1018  ~25     $24
         47        BOOL                                             ~15     ~25
    6    48    >   ASSIGN                                                   !7, ~15
   13    49      > JMPZ                                                     !7, ->79
   14    50    >   ASSIGN                                                   !8, !3
   15    51        ASSIGN                                                   !9, 0
   16    52        STRLEN                                           ~29     !6
         53        ASSIGN                                                   !10, ~29
   17    54        ASSIGN                                                   !11, 0
         55      > JMP                                                      ->73
   18    56    >   FETCH_DIM_R                                      ~32     !6, !11
         57        IS_EQUAL                                                 !4, ~32
         58      > JMPZ                                                     ~33, ->61
   19    59    >   ASSIGN                                                   !9, 1
         60      > JMP                                                      ->70
   20    61    >   BOOL_NOT                                         ~35     !9
         62      > JMPZ_EX                                          ~35     ~35, ->66
         63    >   FETCH_DIM_R                                      ~36     !6, !11
         64        IS_EQUAL                                         ~37     !3, ~36
         65        BOOL                                             ~35     ~37
         66    > > JMPZ                                                     ~35, ->69
   21    67    >   ASSIGN_OP                                     8          !8, !3
         68      > JMP                                                      ->70
   23    69    >   ASSIGN                                                   !9, 0
   25    70    >   FETCH_DIM_R                                      ~40     !6, !11
         71        ASSIGN_OP                                     8          !8, ~40
   17    72        PRE_INC                                                  !11
         73    >   IS_SMALLER                                               !11, !10
         74      > JMPNZ                                                    ~43, ->56
   27    75    >   ASSIGN_OP                                     8          !8, !3
   28    76        CONCAT                                           ~45     !8, !2
         77        ASSIGN_OP                                     8          !5, ~45
         78      > JMP                                                      ->81
   30    79    >   CONCAT                                           ~47     !6, !2
         80        ASSIGN_OP                                     8          !5, ~47
    5    81    > > JMP                                                      ->7
         82    >   FE_FREE                                                  $13
   33    83        INIT_FCALL                                               'substr'
         84        SEND_VAR                                                 !5
         85        SEND_VAL                                                 0
         86        SEND_VAL                                                 -1
         87        DO_ICALL                                         $49     
         88        ASSIGN                                                   !5, $49
   34    89        ASSIGN_OP                                     8          !5, '%0A'
   35    90        INIT_FCALL                                               'fwrite'
         91        SEND_VAR                                                 !0
         92        SEND_VAR                                                 !5
         93        DO_ICALL                                         $52     
         94      > RETURN                                                   $52
   36    95*     > RETURN                                                   null

End of function fputcsv2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
193.5 ms | 1402 KiB | 26 Q