3v4l.org

run code in 500+ 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 (isset($m[$i][2])) { // immovable $result[$index] .= $m[$i][2]; // append to string } else { // movable from front while (--$j >= 0) { if (!isset($m[$j][2])) { // 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/vR6hB
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 = 24, Position 2 = 29
Branch analysis from position: 24
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: 29
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 42, Position 2 = 30
Branch analysis from position: 42
Branch analysis from position: 30
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/vR6hB
function name:  swapLetterPositions
number of ops:  52
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_IS                                         ~18     !5, !6
         22        ISSET_ISEMPTY_DIM_OBJ                             0          ~18, 2
         23      > JMPZ                                                         ~19, ->29
   19    24    >   FETCH_DIM_R                                          ~21     !5, !6
         25        FETCH_DIM_R                                          ~22     ~21, 2
         26        ASSIGN_DIM_OP                    .=               8          !1, !3
         27        OP_DATA                                                      ~22
   18    28      > JMP                                                          ->42
   21    29    > > JMP                                                          ->39
   22    30    >   FETCH_DIM_IS                                         ~23     !5, !7
         31        ISSET_ISEMPTY_DIM_OBJ                             0  ~24     ~23, 2
         32        BOOL_NOT                                             ~25     ~24
         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, ->30
   17    42    >   PRE_INC                                                      !6
         43    >   IS_SMALLER                                                   !6, !4
         44      > JMPNZ                                                        ~32, ->21
   14    45    > > JMP                                                          ->7
         46    >   FE_FREE                                                      $10
   30    47        FRAMELESS_ICALL_2                implode             ~33     '+', !1
         48        VERIFY_RETURN_TYPE                                           ~33
         49      > RETURN                                                       ~33
   31    50*       VERIFY_RETURN_TYPE                                           
         51*     > RETURN                                                       null

End of function swapletterpositions

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
160.57 ms | 1956 KiB | 16 Q