3v4l.org

run code in 300+ PHP versions simultaneously
<?php function inject_hyphens($word, $positions) { $pad = 0; foreach ($positions as $position) { $word = substr($word, 0, $position + $pad) . '-' . substr($word, $position + $pad, strlen($word)); $pad++; } return $word; } $input = array('syl-labification', 'sylla-bification', 'syllabi-fication', 'syllabifi-cation', 'syllabifica-tion', 'syllabification' ); //wanted $output = 'syl-la-bi-fi-ca-tion'; $positions = array(); foreach($input as $word) { preg_match_all('/-/', $word, $matches, PREG_OFFSET_CAPTURE); foreach($matches[0] as $key => $value) { $positions[] = $value[1]; } } $positions = array_unique($positions); var_dump(inject_hyphens('syllabification', $positions));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 20
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 20
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 18
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/GQ6H2
function name:  (null)
number of ops:  33
compiled vars:  !0 = $input, !1 = $positions, !2 = $word, !3 = $matches, !4 = $value, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   ASSIGN                                                   !0, <array>
   24     1        ASSIGN                                                   !1, <array>
   26     2      > FE_RESET_R                                       $8      !0, ->20
          3    > > FE_FETCH_R                                               $8, !2, ->20
   28     4    >   INIT_FCALL                                               'preg_match_all'
          5        SEND_VAL                                                 '%2F-%2F'
          6        SEND_VAR                                                 !2
          7        SEND_REF                                                 !3
          8        SEND_VAL                                                 256
          9        DO_ICALL                                                 
   29    10        FETCH_DIM_R                                      ~10     !3, 0
         11      > FE_RESET_R                                       $11     ~10, ->18
         12    > > FE_FETCH_R                                       ~12     $11, !4, ->18
         13    >   ASSIGN                                                   !5, ~12
   31    14        FETCH_DIM_R                                      ~15     !4, 1
         15        ASSIGN_DIM                                               !1
         16        OP_DATA                                                  ~15
   29    17      > JMP                                                      ->12
         18    >   FE_FREE                                                  $11
   26    19      > JMP                                                      ->3
         20    >   FE_FREE                                                  $8
   35    21        INIT_FCALL                                               'array_unique'
         22        SEND_VAR                                                 !1
         23        DO_ICALL                                         $16     
         24        ASSIGN                                                   !1, $16
   36    25        INIT_FCALL                                               'var_dump'
         26        INIT_FCALL                                               'inject_hyphens'
         27        SEND_VAL                                                 'syllabification'
         28        SEND_VAR                                                 !1
         29        DO_FCALL                                      0  $18     
         30        SEND_VAR                                                 $18
         31        DO_ICALL                                                 
         32      > RETURN                                                   1

Function inject_hyphens:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 23
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 23
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/GQ6H2
function name:  inject_hyphens
number of ops:  26
compiled vars:  !0 = $word, !1 = $positions, !2 = $pad, !3 = $position
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        ASSIGN                                                   !2, 0
    6     3      > FE_RESET_R                                       $5      !1, ->23
          4    > > FE_FETCH_R                                               $5, !3, ->23
    8     5    >   INIT_FCALL                                               'substr'
          6        SEND_VAR                                                 !0
          7        SEND_VAL                                                 0
          8        ADD                                              ~6      !3, !2
          9        SEND_VAL                                                 ~6
         10        DO_ICALL                                         $7      
         11        CONCAT                                           ~8      $7, '-'
         12        INIT_FCALL                                               'substr'
         13        SEND_VAR                                                 !0
         14        ADD                                              ~9      !3, !2
         15        SEND_VAL                                                 ~9
         16        STRLEN                                           ~10     !0
         17        SEND_VAL                                                 ~10
         18        DO_ICALL                                         $11     
         19        CONCAT                                           ~12     ~8, $11
         20        ASSIGN                                                   !0, ~12
    9    21        PRE_INC                                                  !2
    6    22      > JMP                                                      ->4
         23    >   FE_FREE                                                  $5
   11    24      > RETURN                                                   !0
   12    25*     > RETURN                                                   null

End of function inject_hyphens

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
179.46 ms | 1403 KiB | 22 Q