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]
Output for 4.4.6 - 4.4.9, 5.2.2 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
[i][b](This is a paragraph with BBcode.)[/i][/b]
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 5.2.0 - 5.2.1
Warning: preg_match_all(): Compilation failed: unrecognized character after (?< at offset 3 in /in/O8UHO on line 9 Warning: implode(): Bad arguments. in /in/O8UHO on line 13 Warning: preg_match_all(): Compilation failed: unrecognized character after (?< at offset 5 in /in/O8UHO on line 17 Warning: array_unique(): The argument should be an array in /in/O8UHO on line 18 Warning: Invalid argument supplied for foreach() in /in/O8UHO on line 22
Output for 4.4.0 - 4.4.5, 5.0.0 - 5.0.5, 5.1.1 - 5.1.6
Warning: preg_match_all(): Compilation failed: unrecognized character after (?< at offset 3 in /in/O8UHO on line 9 Notice: Undefined index: start in /in/O8UHO on line 12 Notice: Undefined index: paragraph in /in/O8UHO on line 13 Warning: implode(): Bad arguments. in /in/O8UHO on line 13 Warning: preg_match_all(): Compilation failed: unrecognized character after (?< at offset 5 in /in/O8UHO on line 17 Notice: Undefined index: tag in /in/O8UHO on line 18 Warning: array_unique(): The argument should be an array in /in/O8UHO on line 18 Warning: Invalid argument supplied for foreach() in /in/O8UHO on line 22
Output for 5.1.0
Fatal error: fatal flex scanner internal error--end of buffer missed in /in/O8UHO on line 31
Process exited with code 255.
Output for 4.3.11
Warning: preg_match_all(): Compilation failed: unrecognized character after (?< at offset 3 in /in/O8UHO on line 9 Notice: Undefined index: start in /in/O8UHO on line 12 Notice: Undefined index: paragraph in /in/O8UHO on line 13 Warning: implode(): Bad arguments. in /in/O8UHO on line 13 Warning: preg_match_all(): Compilation failed: unrecognized character after (?< at offset 5 in /in/O8UHO on line 17 Notice: Undefined index: tag in /in/O8UHO on line 18 Warning: array_unique(): The argument should be an array in /in/O8UHO on line 18
Output for 4.3.2 - 4.3.10
Warning: Compilation failed: unrecognized character after (?< at offset 3 in /in/O8UHO on line 9 Notice: Undefined index: start in /in/O8UHO on line 12 Notice: Undefined index: paragraph in /in/O8UHO on line 13 Warning: implode(): Bad arguments. in /in/O8UHO on line 13 Warning: Compilation failed: unrecognized character after (?< at offset 5 in /in/O8UHO on line 17 Notice: Undefined index: tag in /in/O8UHO on line 18 Warning: array_unique(): The argument should be an array in /in/O8UHO on line 18 Warning: Invalid argument supplied for foreach() in /in/O8UHO on line 22
Output for 4.3.0 - 4.3.1
Warning: Compilation failed: unrecognized character after (?< at offset 3 in /in/O8UHO on line 9 Notice: Undefined index: start in /in/O8UHO on line 12 Notice: Undefined index: paragraph in /in/O8UHO on line 13 Warning: implode() [http://www.php.net/function.implode]: Bad arguments. in /in/O8UHO on line 13 Warning: Compilation failed: unrecognized character after (?< at offset 5 in /in/O8UHO on line 17 Notice: Undefined index: tag in /in/O8UHO on line 18 Warning: array_unique() [http://www.php.net/function.array-unique]: The argument should be an array in /in/O8UHO on line 18 Warning: Invalid argument supplied for foreach() in /in/O8UHO on line 22

preferences:
229.71 ms | 419 KiB | 5 Q