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; */
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.29, 8.2.0 - 8.2.20, 8.3.0 - 8.3.4, 8.3.6 - 8.3.8
[quote=mickmackusa]Keep this[/quote] Let's remove a second set of quotes [quote=mickmackusa]Let's do a third quote inside of my quote... [/quote] This should be good, but What if ?
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 [quote=mickmackusa]Keep this[/quote] Let's remove a second set of quotes [quote=mickmackusa]Let's do a third quote inside of my quote... [/quote] This should be good, but What if ?

preferences:
144.82 ms | 402 KiB | 161 Q