3v4l.org

run code in 300+ PHP versions simultaneously
<?php $names = [ 'ab1 ab2', // becomes ba1 ba2 'qwerty uçop', // becomes ytrewq poçu 'q1werty% uio*pl', // becomes y1trewq% lpo*iu 'Привет, мир!', // becomes тевирП, рим! 'Hello, dear @user_non-name, congrats100 points*@!', // olleH, raed @eman_non-resu, stragnoc100 stniop*@! 'a' ]; function swapLetterPositions($string): string { $result = []; foreach (explode(' ', $string) as $index => $word) { $result[$index] = ''; $count = preg_match_all('/(\pL)|(.)/u', $word, $m, PREG_SET_ORDER); for ($i = 0, $j = $count; $i < $count; ++$i) { if (!$m[$i][1]) { // immovable $result[$index] .= $m[$i][2]; // append to string } else { // movable from front while (--$j >= 0) { if ($m[$j][1]) { // movable from back $result[$index] .= $m[$j][1]; // append to string break; } } } } } return implode(' ', $result); } foreach ($names as $name) { echo "\"$name\" => \"" . swapLetterPositions($name) . "\"\n---\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 13
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/r0YIe
function name:  (null)
number of ops:  15
compiled vars:  !0 = $names, !1 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   33     1      > FE_RESET_R                                       $3      !0, ->13
          2    > > FE_FETCH_R                                               $3, !1, ->13
   34     3    >   ROPE_INIT                                     3  ~5      '%22'
          4        ROPE_ADD                                      1  ~5      ~5, !1
          5        ROPE_END                                      2  ~4      ~5, '%22+%3D%3E+%22'
          6        INIT_FCALL                                               'swapletterpositions'
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $7      
          9        CONCAT                                           ~8      ~4, $7
         10        CONCAT                                           ~9      ~8, '%22%0A---%0A'
         11        ECHO                                                     ~9
   33    12      > JMP                                                      ->2
         13    >   FE_FREE                                                  $3
   35    14      > RETURN                                                   1

Function swapletterpositions:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 46
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 46
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
2 jumps found. (Code = 44) Position 1 = 45, Position 2 = 21
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 30
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
2 jumps found. (Code = 44) Position 1 = 45, Position 2 = 21
Branch analysis from position: 45
Branch analysis from position: 21
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 42, Position 2 = 31
Branch analysis from position: 42
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 39
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
Branch analysis from position: 39
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
filename:       /in/r0YIe
function name:  swapLetterPositions
number of ops:  55
compiled vars:  !0 = $string, !1 = $result, !2 = $word, !3 = $index, !4 = $count, !5 = $m, !6 = $i, !7 = $j
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   13     1        ASSIGN                                                   !1, <array>
   14     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '+'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $9      
          6      > FE_RESET_R                                       $10     $9, ->46
          7    > > FE_FETCH_R                                       ~11     $10, !2, ->46
          8    >   ASSIGN                                                   !3, ~11
   15     9        ASSIGN_DIM                                               !1, !3
         10        OP_DATA                                                  ''
   16    11        INIT_FCALL                                               'preg_match_all'
         12        SEND_VAL                                                 '%2F%28%5CpL%29%7C%28.%29%2Fu'
         13        SEND_VAR                                                 !2
         14        SEND_REF                                                 !5
         15        SEND_VAL                                                 2
         16        DO_ICALL                                         $14     
         17        ASSIGN                                                   !4, $14
   17    18        ASSIGN                                                   !6, 0
         19        ASSIGN                                                   !7, !4
         20      > JMP                                                      ->43
   18    21    >   FETCH_DIM_R                                      ~18     !5, !6
         22        FETCH_DIM_R                                      ~19     ~18, 1
         23        BOOL_NOT                                         ~20     ~19
         24      > JMPZ                                                     ~20, ->30
   19    25    >   FETCH_DIM_R                                      ~22     !5, !6
         26        FETCH_DIM_R                                      ~23     ~22, 2
         27        ASSIGN_DIM_OP                .=               8          !1, !3
         28        OP_DATA                                                  ~23
   18    29      > JMP                                                      ->42
   21    30    > > JMP                                                      ->39
   22    31    >   FETCH_DIM_R                                      ~24     !5, !7
         32        FETCH_DIM_R                                      ~25     ~24, 1
         33      > JMPZ                                                     ~25, ->39
   23    34    >   FETCH_DIM_R                                      ~27     !5, !7
         35        FETCH_DIM_R                                      ~28     ~27, 1
         36        ASSIGN_DIM_OP                .=               8          !1, !3
         37        OP_DATA                                                  ~28
   24    38      > JMP                                                      ->42
   21    39    >   PRE_DEC                                          ~29     !7
         40        IS_SMALLER_OR_EQUAL                                      0, ~29
         41      > JMPNZ                                                    ~30, ->31
   17    42    >   PRE_INC                                                  !6
         43    >   IS_SMALLER                                               !6, !4
         44      > JMPNZ                                                    ~32, ->21
   14    45    > > JMP                                                      ->7
         46    >   FE_FREE                                                  $10
   30    47        INIT_FCALL                                               'implode'
         48        SEND_VAL                                                 '+'
         49        SEND_VAR                                                 !1
         50        DO_ICALL                                         $33     
         51        VERIFY_RETURN_TYPE                                       $33
         52      > RETURN                                                   $33
   31    53*       VERIFY_RETURN_TYPE                                       
         54*     > RETURN                                                   null

End of function swapletterpositions

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.1 ms | 1015 KiB | 17 Q