3v4l.org

run code in 500+ PHP versions simultaneously
<?php // stackoverflow.com/questions/20348380 function fixJSON($json) { $regex = <<<'REGEX' ~ "[^"\\]*(?:\\.|[^"\\]*)*" (*SKIP)(*F) | '([^'\\]*(?:\\.|[^'\\]*)*)' ~x REGEX; return preg_replace_callback($regex, function($matches) { return '"' . preg_replace('~\\\\.(*SKIP)(*F)|"~', '\\"', $matches[1]) . '"'; }, $json); } $json = "{'id': '3', 'answer': '1'},{'id': '4', 'answer': 'здесь ответ на вопрос, 12345 abc'},{'id': '4', 'answer': '123'}"; $obj = json_decode(fixJSON("[$json]")); foreach ($obj as $key=>$val) { echo "$val->answer\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 19
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 19
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/YhZNp
function name:  (null)
number of ops:  21
compiled vars:  !0 = $json, !1 = $obj, !2 = $val, !3 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   18     0  E >   ASSIGN                                                       !0, '%7B%27id%27%3A+%273%27%2C+%27answer%27%3A+%271%27%7D%2C%7B%27id%27%3A+%274%27%2C+%27answer%27%3A+%27%D0%B7%D0%B4%D0%B5%D1%81%D1%8C+%D0%BE%D1%82%D0%B2%D0%B5%D1%82+%D0%BD%D0%B0+%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81%2C+12345+abc%27%7D%2C%7B%27id%27%3A+%274%27%2C+%27answer%27%3A+%27123%27%7D'
   19     1        INIT_FCALL                                                   'json_decode'
          2        INIT_FCALL                                                   'fixjson'
          3        ROPE_INIT                                         3  ~6      '%5B'
          4        ROPE_ADD                                          1  ~6      ~6, !0
          5        ROPE_END                                          2  ~5      ~6, '%5D'
          6        SEND_VAL                                                     ~5
          7        DO_FCALL                                          0  $8      
          8        SEND_VAR                                                     $8
          9        DO_ICALL                                             $9      
         10        ASSIGN                                                       !1, $9
   20    11      > FE_RESET_R                                           $11     !1, ->19
         12    > > FE_FETCH_R                                           ~12     $11, !2, ->19
         13    >   ASSIGN                                                       !3, ~12
   21    14        FETCH_OBJ_R                                          ~14     !2, 'answer'
         15        NOP                                                          
         16        FAST_CONCAT                                          ~15     ~14, '%0A'
         17        ECHO                                                         ~15
   20    18      > JMP                                                          ->12
         19    >   FE_FREE                                                      $11
   22    20      > RETURN                                                       1

Function fixjson:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YhZNp
function name:  fixJSON
number of ops:  10
compiled vars:  !0 = $json, !1 = $regex
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   RECV                                                 !0      
    5     1        ASSIGN                                                       !1, '%7E%0A++++%22%5B%5E%22%5C%5C%5D%2A%28%3F%3A%5C%5C.%7C%5B%5E%22%5C%5C%5D%2A%29%2A%22%0A++++%28%2ASKIP%29%28%2AF%29%0A++%7C+%27%28%5B%5E%27%5C%5C%5D%2A%28%3F%3A%5C%5C.%7C%5B%5E%27%5C%5C%5D%2A%29%2A%29%27%0A%7Ex'
   13     2        INIT_FCALL                                                   'preg_replace_callback'
          3        SEND_VAR                                                     !1
          4        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
   15     5        SEND_VAL                                                     ~3
          6        SEND_VAR                                                     !0
   13     7        DO_ICALL                                             $4      
   15     8      > RETURN                                                       $4
   16     9*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YhZNp
function name:  {closure:fixJSON():13}
number of ops:  8
compiled vars:  !0 = $matches
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   13     0  E >   RECV                                                 !0      
   14     1        FETCH_DIM_R                                          ~1      !0, 1
          2        FRAMELESS_ICALL_3                preg_replace        ~2      '%7E%5C%5C.%28%2ASKIP%29%28%2AF%29%7C%22%7E', '%5C%22'
          3        OP_DATA                                                      ~1
          4        CONCAT                                               ~3      '%22', ~2
          5        CONCAT                                               ~4      ~3, '%22'
          6      > RETURN                                                       ~4
   15     7*     > RETURN                                                       null

End of Dynamic Function 0

End of function fixjson

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
155.64 ms | 2109 KiB | 16 Q