3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = <<<STR category1_subcat_1 category1_subcat_2 category1_subcat_3 category2_subcat_1 category2_subcat_2 category3_subcat_1 category3_subcat_3 STR; // Explode the string, with a newline as the delimiter $lines = explode("\n", $str); // Init the category array $cats = []; // iterate over the lines foreach($lines as $currentLine) { // Match category{DIGIT} using regular exp preg_match("/category[0-9]/", $currentLine, $match); // Remove the $match from the current line $currentLine = str_replace($match[0], "", $currentLine); // Push the element to the categories array, and also remove the // the underscores (_) $cats[$match[0]][] = str_replace("_", "", $currentLine); } var_dump($cats);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 31
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 31
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/Lv4o4
function name:  (null)
number of ops:  36
compiled vars:  !0 = $str, !1 = $lines, !2 = $cats, !3 = $currentLine, !4 = $match
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'category1_subcat_1%0Acategory1_subcat_2%0Acategory1_subcat_3%0Acategory2_subcat_1%0Acategory2_subcat_2%0Acategory3_subcat_1%0Acategory3_subcat_3'
   14     1        INIT_FCALL                                               'explode'
          2        SEND_VAL                                                 '%0A'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !1, $6
   16     6        ASSIGN                                                   !2, <array>
   18     7      > FE_RESET_R                                       $9      !1, ->31
          8    > > FE_FETCH_R                                               $9, !3, ->31
   20     9    >   INIT_FCALL                                               'preg_match'
         10        SEND_VAL                                                 '%2Fcategory%5B0-9%5D%2F'
         11        SEND_VAR                                                 !3
         12        SEND_REF                                                 !4
         13        DO_ICALL                                                 
   22    14        INIT_FCALL                                               'str_replace'
         15        FETCH_DIM_R                                      ~11     !4, 0
         16        SEND_VAL                                                 ~11
         17        SEND_VAL                                                 ''
         18        SEND_VAR                                                 !3
         19        DO_ICALL                                         $12     
         20        ASSIGN                                                   !3, $12
   25    21        FETCH_DIM_R                                      ~14     !4, 0
         22        INIT_FCALL                                               'str_replace'
         23        SEND_VAL                                                 '_'
         24        SEND_VAL                                                 ''
         25        SEND_VAR                                                 !3
         26        DO_ICALL                                         $17     
         27        FETCH_DIM_W                                      $15     !2, ~14
         28        ASSIGN_DIM                                               $15
         29        OP_DATA                                                  $17
   18    30      > JMP                                                      ->8
         31    >   FE_FREE                                                  $9
   27    32        INIT_FCALL                                               'var_dump'
         33        SEND_VAR                                                 !2
         34        DO_ICALL                                                 
         35      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
290.24 ms | 1014 KiB | 17 Q