Original text:';
echo '
';
print_r($bbcode);
echo '
';
function bbcode2email($text, $wrap_length = 72)
{
// Strip all bbcodes, except the quote bbcodes
$text = preg_replace('%\[/?(?!quote)[a-z]+(?:=[^\]]+)?\]%i', '', $text);
// Match the deepest nested quote bbcode
// An adapted example from Mastering Regular Expressions
$match_quote_regex = '%
\[quote(?:=([^\]]+))?\]
(
(?>[^\[]*)
(?>
(?!\[/?quote(?:=[^\]]+)?\])
\[
[^\[]*
)*
)
\[/quote\]
%ix';
while (preg_match($match_quote_regex, $text, $matches))
{
$quote = preg_replace(
array('%^(?=\>)%m', '%^(?!\>)%m'),
array('>', '> '),
$matches[1]." said:\n".$matches[2]);
$text = str_replace($matches[0], $quote, $text);
}
// Wrap lines if $wrap_length is higher than -1
if ($wrap_length > -1)
{
// Split all lines and wrap them indivitually
$parts = explode("\n", $text);
foreach ($parts as $k => $part)
{
preg_match('%^(>+ )?(.*)%', $part, $matches);
$parts[$k] = wordwrap($matches[1].$matches[2], $wrap_length -
strlen($matches[1]), "\n".$matches[1]);
}
return implode("\n", $parts);
}
else
return $text;
}
echo 'Converted text:
';
echo '';
print_r(bbcode2email($bbcode));
echo '
';