3v4l.org

run code in 300+ PHP versions simultaneously
<?php $bb = '[i][b][i][b](This is a paragraph with BBcode.)[/b][/i][/b][/i]'; // regex with start, paragraph, and end capture groups $regex = '#(?<start>(\[[a-z]*\])*+)(?<paragraph>.*)(?<end>(\[\/[a-z]*\])*+)#U'; // put matches into $matches array preg_match_all($regex, $bb, $matches); // get the stuff we need $start = $matches['start'][0]; // string(12) "[i][b][i][b]" $paragraph = implode('', $matches['paragraph']); // now we will grab each tag $regex = '#\[(?<tag>[a-z])\]#'; preg_match_all($regex, $start, $matches); $tags = array_unique($matches['tag']); // and build up the new string $newString = ''; foreach($tags as $tag) { $newString .= '[' . $tag . ']'; } // create the end tags $end = str_replace('[', '[/', $newString); // put it all together $newString .= $paragraph . $end; echo $newString; // [i][b](This is a paragraph with BBcode.)[/i][/b]
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 29, Position 2 = 34
Branch analysis from position: 29
2 jumps found. (Code = 78) Position 1 = 30, Position 2 = 34
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/O8UHO
function name:  (null)
number of ops:  45
compiled vars:  !0 = $bb, !1 = $regex, !2 = $matches, !3 = $start, !4 = $paragraph, !5 = $tags, !6 = $newString, !7 = $tag, !8 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%5Bi%5D%5Bb%5D%5Bi%5D%5Bb%5D%28This+is+a+paragraph+with+BBcode.%29%5B%2Fb%5D%5B%2Fi%5D%5B%2Fb%5D%5B%2Fi%5D'
    6     1        ASSIGN                                                   !1, '%23%28%3F%3Cstart%3E%28%5C%5B%5Ba-z%5D%2A%5C%5D%29%2A%2B%29%28%3F%3Cparagraph%3E.%2A%29%28%3F%3Cend%3E%28%5C%5B%5C%2F%5Ba-z%5D%2A%5C%5D%29%2A%2B%29%23U'
    9     2        INIT_FCALL                                               'preg_match_all'
          3        SEND_VAR                                                 !1
          4        SEND_VAR                                                 !0
          5        SEND_REF                                                 !2
          6        DO_ICALL                                                 
   12     7        FETCH_DIM_R                                      ~12     !2, 'start'
          8        FETCH_DIM_R                                      ~13     ~12, 0
          9        ASSIGN                                                   !3, ~13
   13    10        INIT_FCALL                                               'implode'
         11        SEND_VAL                                                 ''
         12        FETCH_DIM_R                                      ~15     !2, 'paragraph'
         13        SEND_VAL                                                 ~15
         14        DO_ICALL                                         $16     
         15        ASSIGN                                                   !4, $16
   16    16        ASSIGN                                                   !1, '%23%5C%5B%28%3F%3Ctag%3E%5Ba-z%5D%29%5C%5D%23'
   17    17        INIT_FCALL                                               'preg_match_all'
         18        SEND_VAR                                                 !1
         19        SEND_VAR                                                 !3
         20        SEND_REF                                                 !2
         21        DO_ICALL                                                 
   18    22        INIT_FCALL                                               'array_unique'
         23        FETCH_DIM_R                                      ~20     !2, 'tag'
         24        SEND_VAL                                                 ~20
         25        DO_ICALL                                         $21     
         26        ASSIGN                                                   !5, $21
   21    27        ASSIGN                                                   !6, ''
   22    28      > FE_RESET_R                                       $24     !5, ->34
         29    > > FE_FETCH_R                                               $24, !7, ->34
   23    30    >   CONCAT                                           ~25     '%5B', !7
         31        CONCAT                                           ~26     ~25, '%5D'
         32        ASSIGN_OP                                     8          !6, ~26
   22    33      > JMP                                                      ->29
         34    >   FE_FREE                                                  $24
   26    35        INIT_FCALL                                               'str_replace'
         36        SEND_VAL                                                 '%5B'
         37        SEND_VAL                                                 '%5B%2F'
         38        SEND_VAR                                                 !6
         39        DO_ICALL                                         $28     
         40        ASSIGN                                                   !8, $28
   29    41        CONCAT                                           ~30     !4, !8
         42        ASSIGN_OP                                     8          !6, ~30
   31    43        ECHO                                                     !6
         44      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.42 ms | 1018 KiB | 17 Q