3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Ajgl\Csv; class SplFileObject extends \SplFileObject { use CsvFileObjectTrait; public function __construct($filename, $open_mode='r', $use_include_path=false, $context=null) { parent::__construct($filename, $open_mode, $use_include_path, $context); $this->fixCsvControl(); } } class SplTempFileObject extends \SplTempFileObject { use CsvFileObjectTrait; public function __construct ($max_memory=null) { parent::__construct($max_memory); $this->fixCsvControl(); } } trait CsvFileObjectTrait { protected function fixCsvControl() { list($delimiter, $enclosure) = $this->getCsvControl(); $this->setCsvControl($delimiter, $enclosure); } protected function checkEscapeChar($enclosure, $escape) { check_csv_escape($enclosure, $escape); } public function fgetcsv($delimiter=",", $enclosure='"', $escape='"') { $this->checkEscapeChar($enclosure, $escape); return parent::fgetcsv($delimiter, $enclosure, $enclosure); } public function fputcsv($fields, $delimiter=',', $enclosure='"', $escape='"') { $this->checkEscapeChar($enclosure, $escape); $this->fwrite(str_putcsv_rfc($fields, $delimiter, $enclosure)); } public function setCsvControl($delimiter=",", $enclosure='"', $escape='"') { $this->checkEscapeChar($enclosure, $escape); parent::setCsvControl($delimiter, $enclosure, $enclosure); } } function fputcsv($handle, $fields, $delimiter=',', $enclosure='"', $escape='"') { check_csv_escape($enclosure, $escape); \fwrite($handle, str_putcsv_rfc($fields, $delimiter, $enclosure)); } function fgetcsv($handle, $length=0, $delimiter=",", $enclosure='"', $escape='"') { check_csv_escape($enclosure, $escape); return \fgetcsv($handle, $length, $delimiter, $enclosure, $enclosure); } function str_getcsv($input, $delimiter=",", $enclosure='"', $escape='"') { check_csv_escape($enclosure, $escape); return \str_getcsv($input, $delimiter, $enclosure, $enclosure); } function str_putcsv($fields, $delimiter=",", $enclosure='"') { $file = new \SplTempFileObject(); $file->fputcsv($fields, $delimiter, $enclosure); $file->rewind(); $line = ''; while (!$file->eof()) { $line .= $file->fgets(); } return \str_replace('\\'.$enclosure, '\\'.$enclosure.$enclosure, $line); } function check_csv_escape($enclosure, $escape) { if ($enclosure !== $escape) { trigger_error( sprintf( "The 'escape' and 'enclosure' chars must be equals. The given 'escape' char '%s' will be ignored.", $escape ), E_USER_WARNING ); } } $a = new SplTempFileObject(); $a->fputcsv(array('Hello \"World"!')); $a->rewind(); var_dump($a->fread(4096));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  (null)
number of ops:  17
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   DECLARE_CLASS                                            'ajgl%5Ccsv%5Csplfileobject', 'splfileobject'
   15     1        DECLARE_CLASS                                            'ajgl%5Ccsv%5Cspltempfileobject', 'spltempfileobject'
  103     2        NEW                                              $1      'Ajgl%5CCsv%5CSplTempFileObject'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $1
  104     5        INIT_METHOD_CALL                                         !0, 'fputcsv'
          6        SEND_VAL_EX                                              <array>
          7        DO_FCALL                                      0          
  105     8        INIT_METHOD_CALL                                         !0, 'rewind'
          9        DO_FCALL                                      0          
  106    10        INIT_NS_FCALL_BY_NAME                                    'Ajgl%5CCsv%5Cvar_dump'
         11        INIT_METHOD_CALL                                         !0, 'fread'
         12        SEND_VAL_EX                                              4096
         13        DO_FCALL                                      0  $6      
         14        SEND_VAR_NO_REF_EX                                       $6
         15        DO_FCALL                                      0          
         16      > RETURN                                                   1

Function ajgl%5Ccsv%5Cfputcsv:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  Ajgl\Csv\fputcsv
number of ops:  19
compiled vars:  !0 = $handle, !1 = $fields, !2 = $delimiter, !3 = $enclosure, !4 = $escape
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      '%2C'
          3        RECV_INIT                                        !3      '%22'
          4        RECV_INIT                                        !4      '%22'
   60     5        INIT_NS_FCALL_BY_NAME                                    'Ajgl%5CCsv%5Ccheck_csv_escape'
          6        SEND_VAR_EX                                              !3
          7        SEND_VAR_EX                                              !4
          8        DO_FCALL                                      0          
   61     9        INIT_FCALL                                               'fwrite'
         10        SEND_VAR                                                 !0
         11        INIT_NS_FCALL_BY_NAME                                    'Ajgl%5CCsv%5Cstr_putcsv_rfc'
         12        SEND_VAR_EX                                              !1
         13        SEND_VAR_EX                                              !2
         14        SEND_VAR_EX                                              !3
         15        DO_FCALL                                      0  $6      
         16        SEND_VAR                                                 $6
         17        DO_ICALL                                                 
   62    18      > RETURN                                                   null

End of function ajgl%5Ccsv%5Cfputcsv

Function ajgl%5Ccsv%5Cfgetcsv:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  Ajgl\Csv\fgetcsv
number of ops:  18
compiled vars:  !0 = $handle, !1 = $length, !2 = $delimiter, !3 = $enclosure, !4 = $escape
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
          2        RECV_INIT                                        !2      '%2C'
          3        RECV_INIT                                        !3      '%22'
          4        RECV_INIT                                        !4      '%22'
   66     5        INIT_NS_FCALL_BY_NAME                                    'Ajgl%5CCsv%5Ccheck_csv_escape'
          6        SEND_VAR_EX                                              !3
          7        SEND_VAR_EX                                              !4
          8        DO_FCALL                                      0          
   67     9        INIT_FCALL                                               'fgetcsv'
         10        SEND_VAR                                                 !0
         11        SEND_VAR                                                 !1
         12        SEND_VAR                                                 !2
         13        SEND_VAR                                                 !3
         14        SEND_VAR                                                 !3
         15        DO_ICALL                                         $6      
         16      > RETURN                                                   $6
   68    17*     > RETURN                                                   null

End of function ajgl%5Ccsv%5Cfgetcsv

Function ajgl%5Ccsv%5Cstr_getcsv:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  Ajgl\Csv\str_getcsv
number of ops:  16
compiled vars:  !0 = $input, !1 = $delimiter, !2 = $enclosure, !3 = $escape
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      '%2C'
          2        RECV_INIT                                        !2      '%22'
          3        RECV_INIT                                        !3      '%22'
   72     4        INIT_NS_FCALL_BY_NAME                                    'Ajgl%5CCsv%5Ccheck_csv_escape'
          5        SEND_VAR_EX                                              !2
          6        SEND_VAR_EX                                              !3
          7        DO_FCALL                                      0          
   73     8        INIT_FCALL                                               'str_getcsv'
          9        SEND_VAR                                                 !0
         10        SEND_VAR                                                 !1
         11        SEND_VAR                                                 !2
         12        SEND_VAR                                                 !2
         13        DO_ICALL                                         $5      
         14      > RETURN                                                   $5
   74    15*     > RETURN                                                   null

End of function ajgl%5Ccsv%5Cstr_getcsv

Function ajgl%5Ccsv%5Cstr_putcsv:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 15
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 15
Branch analysis from position: 22
Branch analysis from position: 15
filename:       /in/LeSBc
function name:  Ajgl\Csv\str_putcsv
number of ops:  32
compiled vars:  !0 = $fields, !1 = $delimiter, !2 = $enclosure, !3 = $file, !4 = $line
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      '%2C'
          2        RECV_INIT                                        !2      '%22'
   78     3        NEW                                              $5      'SplTempFileObject'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !3, $5
   79     6        INIT_METHOD_CALL                                         !3, 'fputcsv'
          7        SEND_VAR_EX                                              !0
          8        SEND_VAR_EX                                              !1
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0          
   80    11        INIT_METHOD_CALL                                         !3, 'rewind'
         12        DO_FCALL                                      0          
   82    13        ASSIGN                                                   !4, ''
   83    14      > JMP                                                      ->18
   84    15    >   INIT_METHOD_CALL                                         !3, 'fgets'
         16        DO_FCALL                                      0  $11     
         17        ASSIGN_OP                                     8          !4, $11
   83    18    >   INIT_METHOD_CALL                                         !3, 'eof'
         19        DO_FCALL                                      0  $13     
         20        BOOL_NOT                                         ~14     $13
         21      > JMPNZ                                                    ~14, ->15
   87    22    >   INIT_FCALL                                               'str_replace'
         23        CONCAT                                           ~15     '%5C', !2
         24        SEND_VAL                                                 ~15
         25        CONCAT                                           ~16     '%5C', !2
         26        CONCAT                                           ~17     ~16, !2
         27        SEND_VAL                                                 ~17
         28        SEND_VAR                                                 !4
         29        DO_ICALL                                         $18     
         30      > RETURN                                                   $18
   88    31*     > RETURN                                                   null

End of function ajgl%5Ccsv%5Cstr_putcsv

Function ajgl%5Ccsv%5Ccheck_csv_escape:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/LeSBc
function name:  Ajgl\Csv\check_csv_escape
number of ops:  14
compiled vars:  !0 = $enclosure, !1 = $escape
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   92     2        IS_NOT_IDENTICAL                                         !0, !1
          3      > JMPZ                                                     ~2, ->13
   93     4    >   INIT_NS_FCALL_BY_NAME                                    'Ajgl%5CCsv%5Ctrigger_error'
   94     5        INIT_NS_FCALL_BY_NAME                                    'Ajgl%5CCsv%5Csprintf'
   95     6        SEND_VAL_EX                                              'The+%27escape%27+and+%27enclosure%27+chars+must+be+equals.+The+given+%27escape%27+char+%27%25s%27+will+be+ignored.'
          7        SEND_VAR_EX                                              !1
   94     8        DO_FCALL                                      0  $3      
   95     9        SEND_VAR_NO_REF_EX                                       $3
   98    10        FETCH_CONSTANT                                   ~4      'Ajgl%5CCsv%5CE_USER_WARNING'
         11        SEND_VAL_EX                                              ~4
   93    12        DO_FCALL                                      0          
  101    13    > > RETURN                                                   null

End of function ajgl%5Ccsv%5Ccheck_csv_escape

Class Ajgl\Csv\SplFileObject:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  __construct
number of ops:  13
compiled vars:  !0 = $filename, !1 = $open_mode, !2 = $use_include_path, !3 = $context
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'r'
          2        RECV_INIT                                        !2      <false>
          3        RECV_INIT                                        !3      null
   10     4        INIT_STATIC_METHOD_CALL                                  
          5        SEND_VAR_EX                                              !0
          6        SEND_VAR_EX                                              !1
          7        SEND_VAR_EX                                              !2
          8        SEND_VAR_EX                                              !3
          9        DO_FCALL                                      0          
   11    10        INIT_METHOD_CALL                                         'fixCsvControl'
         11        DO_FCALL                                      0          
   12    12      > RETURN                                                   null

End of function __construct

End of class Ajgl\Csv\SplFileObject.

Class Ajgl\Csv\SplTempFileObject:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  __construct
number of ops:  7
compiled vars:  !0 = $max_memory
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV_INIT                                        !0      null
   21     1        INIT_STATIC_METHOD_CALL                                  
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
   22     4        INIT_METHOD_CALL                                         'fixCsvControl'
          5        DO_FCALL                                      0          
   23     6      > RETURN                                                   null

End of function __construct

End of class Ajgl\Csv\SplTempFileObject.

Class Ajgl\Csv\CsvFileObjectTrait:
Function fixcsvcontrol:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  fixCsvControl
number of ops:  12
compiled vars:  !0 = $delimiter, !1 = $enclosure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   INIT_METHOD_CALL                                         'getCsvControl'
          1        DO_FCALL                                      0  $2      
          2        FETCH_LIST_R                                     $3      $2, 0
          3        ASSIGN                                                   !0, $3
          4        FETCH_LIST_R                                     $5      $2, 1
          5        ASSIGN                                                   !1, $5
          6        FREE                                                     $2
   31     7        INIT_METHOD_CALL                                         'setCsvControl'
          8        SEND_VAR_EX                                              !0
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0          
   32    11      > RETURN                                                   null

End of function fixcsvcontrol

Function checkescapechar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  checkEscapeChar
number of ops:  7
compiled vars:  !0 = $enclosure, !1 = $escape
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   36     2        INIT_NS_FCALL_BY_NAME                                    'Ajgl%5CCsv%5Ccheck_csv_escape'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
   37     6      > RETURN                                                   null

End of function checkescapechar

Function fgetcsv:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  fgetcsv
number of ops:  14
compiled vars:  !0 = $delimiter, !1 = $enclosure, !2 = $escape
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV_INIT                                        !0      '%2C'
          1        RECV_INIT                                        !1      '%22'
          2        RECV_INIT                                        !2      '%22'
   41     3        INIT_METHOD_CALL                                         'checkEscapeChar'
          4        SEND_VAR_EX                                              !1
          5        SEND_VAR_EX                                              !2
          6        DO_FCALL                                      0          
   42     7        INIT_STATIC_METHOD_CALL                                  'fgetcsv'
          8        SEND_VAR_EX                                              !0
          9        SEND_VAR_EX                                              !1
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0  $4      
         12      > RETURN                                                   $4
   43    13*     > RETURN                                                   null

End of function fgetcsv

Function fputcsv:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  fputcsv
number of ops:  17
compiled vars:  !0 = $fields, !1 = $delimiter, !2 = $enclosure, !3 = $escape
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      '%2C'
          2        RECV_INIT                                        !2      '%22'
          3        RECV_INIT                                        !3      '%22'
   47     4        INIT_METHOD_CALL                                         'checkEscapeChar'
          5        SEND_VAR_EX                                              !2
          6        SEND_VAR_EX                                              !3
          7        DO_FCALL                                      0          
   48     8        INIT_METHOD_CALL                                         'fwrite'
          9        INIT_NS_FCALL_BY_NAME                                    'Ajgl%5CCsv%5Cstr_putcsv_rfc'
         10        SEND_VAR_EX                                              !0
         11        SEND_VAR_EX                                              !1
         12        SEND_VAR_EX                                              !2
         13        DO_FCALL                                      0  $5      
         14        SEND_VAR_NO_REF_EX                                       $5
         15        DO_FCALL                                      0          
   49    16      > RETURN                                                   null

End of function fputcsv

Function setcsvcontrol:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LeSBc
function name:  setCsvControl
number of ops:  13
compiled vars:  !0 = $delimiter, !1 = $enclosure, !2 = $escape
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV_INIT                                        !0      '%2C'
          1        RECV_INIT                                        !1      '%22'
          2        RECV_INIT                                        !2      '%22'
   53     3        INIT_METHOD_CALL                                         'checkEscapeChar'
          4        SEND_VAR_EX                                              !1
          5        SEND_VAR_EX                                              !2
          6        DO_FCALL                                      0          
   54     7        INIT_STATIC_METHOD_CALL                                  'setCsvControl'
          8        SEND_VAR_EX                                              !0
          9        SEND_VAR_EX                                              !1
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0          
   55    12      > RETURN                                                   null

End of function setcsvcontrol

End of class Ajgl\Csv\CsvFileObjectTrait.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
239.66 ms | 1028 KiB | 26 Q