Remove bbcode size from post text

349 views Asked by At

Forgive me for not being better at regular expressions.

Apologies: example is updated

I am cleaning up some garbage in a phpBB forum with a PHP script. phpBB encodes bbcode with some sort of key, an 8 digit string that appears after a colon. Here is an example:

outer [size=5:24f81ld3]inner text[/size:24f81ld3] outer

I am trying to strip out where the size is one character, not two, so the above would become:

outer inner text outer

But this:

outer [size=100:24f81ld3]inner text[/size:24f81ld3] outer

Would NOT become this:

outer inner text outer

Appreciate any help!

2

There are 2 answers

6
Alex Shesterov On BEST ANSWER

Here's the code

function remove_phpbb_garbage($str)
{
  return preg_replace('/\[size=[0-9a-z](?::[0-9a-z]+)?\]((?:[^\[]|\[(?!\/size))*)\[\/size(?::[0-9a-z]+)?\]/i', '\1', $str);
}

Here are the test use cases:

echo remove_phpbb_garbage("outer [size=1]inner text[/size] outer"), "\n";
echo remove_phpbb_garbage("outer [size=5:24f81ld3]inner text[/size] outer"), "\n";
echo remove_phpbb_garbage("outer [size=100:24f81ld3]inner text[/size] outer"), "\n";
echo remove_phpbb_garbage("outer [size=100:24f81ld3]inner text[/size] outer"), "\n";
echo remove_phpbb_garbage("outer [size=5:24f81ld3]inner text[/size:24f81ld3] outer"), "\n";
echo remove_phpbb_garbage("outer [size=100:24f81ld3]inner text[/size:24f81ld3] outer"), "\n";
echo remove_phpbb_garbage("[b:2q9o3yfx][size=2:2q9o3yfx][font=Arial:2q9o3yfx]Please be careful on how to Jail break your Iphone. In case if you are not already did it. There is a lot of specific instructions How to Jail break your phone on the web. Please read up before attempting a Jail break.[/font:2q9o3yfx][/size:2q9o3yfx][/b:2q9o3yfx] _________________ [bg2ads1:2q9o3yfx][/bg2ads1:2q9o3yfx]"), "\n";

Here's the output:

outer inner text outer
outer inner text outer
outer [size=100:24f81ld3]inner text[/size] outer
outer [size=100:24f81ld3]inner text[/size] outer
outer inner text outer
outer [size=100:24f81ld3]inner text[/size:24f81ld3] outer
[b:2q9o3yfx][font=Arial:2q9o3yfx]Please be careful on how to Jail break your Iphone. In case if you are not already did it. There is a lot of specific instructions How to Jail break your phone on the web. Please read up before attempting a Jail break.[/font:2q9o3yfx][/b:2q9o3yfx] _________________ [bg2ads1:2q9o3yfx][/bg2ads1:2q9o3yfx]

Important note: The function does not handle nested size tags correctly!


If you're interested in regular expressions, this wonderful resource covers them in all details and flavors: http://www.regular-expressions.info/

3
Avinash Raj On

The below regex would match the size tag only if it's value is exactly one character.

Regex:

\[(size)=[^:]:([^\]\[]*)\]([^\[\]]*)\[\/\1:\2]

Replacement string:

$3

DEMO

Code would be,

preg_replace('~\[(size)=[^:]:([^\]\[]*)\]([^\[\]]*)\[\/\1:\2]~', '$3', $str);