3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* This function DOES NOT validate the $bbcode string to contain a balanced number of opening & closing tags. This funcion DOES check that there are enough closing tags to conclude a targeted opening tag. */ function omit_user_quotes($bbcode, $user) { $substrings = preg_split('~(\[/?quote[^]]*\])~', $bbcode, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $opens = 0; // necessary declaration to avoid Notice when no quote tags in $bbcode string foreach ($substrings as $index => $substring) { if (!isset($start) && $substring == "[quote={$user}]") { // found targeted user's first opening quote $start = $index; // disqualify the first if statement and start searching for end tag $opens = 1; // $opens counts how many end tags are required to conclude quote block } elseif (isset($start)) { if (strpos($substring, '[quote=') !== false) { // if a nested opening quote tag is found ++$opens; // increment and continue looking for closing quote tags } elseif (strpos($substring, '[/quote]') !== false) { // if a closing quote tag is found --$opens; // decrement and check for quote tag conclusion or error if (!$opens) { // if $opens is zero ($opens can never be less than zero) $substrings = array_diff_key($substrings, array_flip(range($start, $index))); // slice away unwanted elements from input array unset($start); // re-qualify the first if statement to allow the process to repeat } } } } if ($opens) { // if $opens is positive return 'Error due to opening/closing tag imbalance (too few end tags)'; } else { return trim(implode($substrings)); // trims the whitespaces on either side of $bbcode string as feature } } /* Single unwanted quote with nested innocent quote: */ /*$bbcode='[quote=testuser] [quote=anotheruser]a sdasdsa dfv rdfgrgre gzdf vrdg[/quote] sdfsd fdsf dsf sdf[/quote] the rest of the test'; */ /* output: the rest of the test */ /* Complex battery of unwanted, wanted, and nested quotes: */ $bbcode = '[quote=mickmackusa]Keep this[/quote] [quote=testuser]Don\'t keep this because [quote=mickmackusa]said don\'t do it[/quote] ... like that\'s a good reason [quote=NaughtySquid] It\'s tricky business, no?[/quote] [quote=nester][quote=nesty][quote=nested][/quote][/quote][/quote] [/quote] Let\'s remove a second set of quotes [quote=testuser]Another quote block[/quote] [quote=mickmackusa]Let\'s do a third quote inside of my quote... [quote=testuser]Another quote block[/quote] [/quote] This should be good, but What if [quote=testuser]quotes himself [quote=testuser] inside of his own[/quote] quote[/quote]?'; /* No quotes: */ //$bbcode='This has no bbcode quote tags in it.'; /* output: This has no bbcode quote tags in it. */ /* Too few end quote tags by innocent user: (No flag is raised because the targeted user has not quoted any text) */ //$bbcode='This [quote=mickmackusa] has not end tag.'; /* output: This [quote=mickmackusa] has not end tag. */ /* Too few end quote tags by unwanted user: */ //$bbcode='This [quote=testuser] has not end tag.'; /* output: Error due to opening/closing tag imbalance (too few end tags) */ /* Too many end quote tags by unwanted user: (No flag is raised because the function does not validate the bbcode text as fully balanced) */ //$bbcode='This [quote=testuser] has too many end[/quote] tags.[/quote]'; /* output: This tags.[/quote] */ $user = 'testuser'; echo omit_user_quotes($bbcode, $user); // omit a single user's quote blocks /* Or if you want to omit quote blocks from multiple users, you can use a loop: $users = ['mickmackusa', 'NaughtySquid']; foreach ($users as $user) { $bbcode = omit_user_quotes($bbcode, $user); } echo $bbcode; */
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GK1hi
function name:  (null)
number of ops:  8
compiled vars:  !0 = $bbcode, !1 = $user
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   ASSIGN                                                   !0, '%5Bquote%3Dmickmackusa%5DKeep+this%5B%2Fquote%5D%0A%5Bquote%3Dtestuser%5DDon%27t+keep+this+because+%0A++++%5Bquote%3Dmickmackusa%5Dsaid+don%27t+do+it%5B%2Fquote%5D%0A++++...+like+that%27s+a+good+reason%0A++++%5Bquote%3DNaughtySquid%5D+It%27s+tricky+business%2C+no%3F%5B%2Fquote%5D%0A++++%5Bquote%3Dnester%5D%5Bquote%3Dnesty%5D%5Bquote%3Dnested%5D%5B%2Fquote%5D%5B%2Fquote%5D%5B%2Fquote%5D%0A%5B%2Fquote%5D%0ALet%27s+remove+a+second+set+of+quotes%0A%5Bquote%3Dtestuser%5DAnother+quote+block%5B%2Fquote%5D%0A%5Bquote%3Dmickmackusa%5DLet%27s+do+a+third+quote+inside+of+my+quote...%0A%5Bquote%3Dtestuser%5DAnother+quote+block%5B%2Fquote%5D%0A%5B%2Fquote%5D%0AThis+should+be+good%2C+but%0AWhat+if+%5Bquote%3Dtestuser%5Dquotes+himself+%5Bquote%3Dtestuser%5D+inside+of+his+own%5B%2Fquote%5D+quote%5B%2Fquote%5D%3F'
   74     1        ASSIGN                                                   !1, 'testuser'
   76     2        INIT_FCALL                                               'omit_user_quotes'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
   84     7      > RETURN                                                   1

Function omit_user_quotes:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 58
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 58
Branch analysis from position: 12
2 jumps found. (Code = 46) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 25
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 57
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 35
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 57
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 57
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 57
Branch analysis from position: 57
Branch analysis from position: 57
Branch analysis from position: 21
Branch analysis from position: 58
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 62
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 58
filename:       /in/GK1hi
function name:  omit_user_quotes
number of ops:  70
compiled vars:  !0 = $bbcode, !1 = $user, !2 = $substrings, !3 = $opens, !4 = $substring, !5 = $index, !6 = $start
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    8     2        INIT_FCALL                                               'preg_split'
          3        SEND_VAL                                                 '%7E%28%5C%5B%2F%3Fquote%5B%5E%5D%5D%2A%5C%5D%29%7E'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 0
          6        SEND_VAL                                                 3
          7        DO_ICALL                                         $7      
          8        ASSIGN                                                   !2, $7
    9     9        ASSIGN                                                   !3, 0
   10    10      > FE_RESET_R                                       $10     !2, ->58
         11    > > FE_FETCH_R                                       ~11     $10, !4, ->58
         12    >   ASSIGN                                                   !5, ~11
   11    13        ISSET_ISEMPTY_CV                                 ~13     !6
         14        BOOL_NOT                                         ~14     ~13
         15      > JMPZ_EX                                          ~14     ~14, ->21
         16    >   ROPE_INIT                                     3  ~16     '%5Bquote%3D'
         17        ROPE_ADD                                      1  ~16     ~16, !1
         18        ROPE_END                                      2  ~15     ~16, '%5D'
         19        IS_EQUAL                                         ~18     !4, ~15
         20        BOOL                                             ~14     ~18
         21    > > JMPZ                                                     ~14, ->25
   12    22    >   ASSIGN                                                   !6, !5
   13    23        ASSIGN                                                   !3, 1
         24      > JMP                                                      ->57
   14    25    >   ISSET_ISEMPTY_CV                                         !6
         26      > JMPZ                                                     ~21, ->57
   15    27    >   INIT_FCALL                                               'strpos'
         28        SEND_VAR                                                 !4
         29        SEND_VAL                                                 '%5Bquote%3D'
         30        DO_ICALL                                         $22     
         31        TYPE_CHECK                                  1018          $22
         32      > JMPZ                                                     ~23, ->35
   16    33    >   PRE_INC                                                  !3
         34      > JMP                                                      ->57
   17    35    >   INIT_FCALL                                               'strpos'
         36        SEND_VAR                                                 !4
         37        SEND_VAL                                                 '%5B%2Fquote%5D'
         38        DO_ICALL                                         $25     
         39        TYPE_CHECK                                  1018          $25
         40      > JMPZ                                                     ~26, ->57
   18    41    >   PRE_DEC                                                  !3
   19    42        BOOL_NOT                                         ~28     !3
         43      > JMPZ                                                     ~28, ->57
   20    44    >   INIT_FCALL                                               'array_diff_key'
         45        SEND_VAR                                                 !2
         46        INIT_FCALL                                               'array_flip'
         47        INIT_FCALL                                               'range'
         48        SEND_VAR                                                 !6
         49        SEND_VAR                                                 !5
         50        DO_ICALL                                         $29     
         51        SEND_VAR                                                 $29
         52        DO_ICALL                                         $30     
         53        SEND_VAR                                                 $30
         54        DO_ICALL                                         $31     
         55        ASSIGN                                                   !2, $31
   21    56        UNSET_CV                                                 !6
   10    57    > > JMP                                                      ->11
         58    >   FE_FREE                                                  $10
   26    59      > JMPZ                                                     !3, ->62
   27    60    > > RETURN                                                   'Error+due+to+opening%2Fclosing+tag+imbalance+%28too+few+end+tags%29'
         61*       JMP                                                      ->69
   29    62    >   INIT_FCALL                                               'trim'
         63        INIT_FCALL                                               'implode'
         64        SEND_VAR                                                 !2
         65        DO_ICALL                                         $33     
         66        SEND_VAR                                                 $33
         67        DO_ICALL                                         $34     
         68      > RETURN                                                   $34
   31    69*     > RETURN                                                   null

End of function omit_user_quotes

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.36 ms | 1420 KiB | 28 Q