I need to remove all whitespace between adjacent BBCode tags in a string without affecting whitespaces in a BBCode's text. The input is something like:
*[ul]
[li]List Item 1[/li]
[li]List Item 2[/li]
[/ul]*
After removing the new lines and tabs, it looks like this:
[ul] [li]List Item 1[/li] [li]List Item 2[/li] [/ul]
To ensure that the whitespace doesn't interfere with the code, I need to remove all whitespace between the commands ([ul]
, [li]
, [/ul]
, [/li]
). How can I achieve this?
You could do something like this, using a regex and preg_replace()
:
$text = preg_replace('/\[(.*?)\]\s*\[/', '[\1][', $text);
You can visualize how this regex works here.