Help - Search - Members - Calendar
Full Version: BBCode parser
Invision Power Services > Community Forums > Community Web Design and Coding
Stephen
I'm trying to build a BBCode parser, but I'm not that good at RegExp. I have the following so far

CODE
final protected function parseBody($data) {
// First clean up the data
$data = nl2br(htmlspecialchars(trim($data)));

// Make links 'clickable
$data = preg_replace('/(^|\s)(http|https|news|ftp):\/\/([^\s]*)/i', '<a href="\\2://\\3" title="Visit \\2://\\3">\\2://\\3</a>', $data);

// Patterns to replace
$patterns = array('/\[b\](.*?)\[\/b\]/i',
'/\[u\](.*?)\[\/u\]/i',
'/\[i\](.*?)\[\/i\]/i',
'/\[url=(.*?)\](.*?)\[\/url\]/i',
'/\[url\](.*?)\[\/url\]/i',
'/\[img\](.*?)\[\/img\]/i',
'/\[color=(.*?)\](.*?)\[\/color\]/i',
'/\[font=(.*?)\](.*?)\[\/font\]/i',
'/\[size=(.*?)\](.*?)\[\/size\]/i'
);

// Text to replace
$replacements = array('<strong>\\1</strong>',
'<u>\\1</u>',
'<em>\\1</em>',
'<a href="\\1" title="Visit \\2">\\2</a>',
'<a href="\\1" title="Visit \\1">\\1</a>',
'<img src="\\1" alt="Posted image" />',
'<span style="color: \\1">\\2</span>',
'<span style="font-family: \\1">\\2</span>',
'<span style="font-size: \\1">\\2</span>'
);

return preg_replace($patterns, $replacements, $data);
}


Can someone help me at [ list ] tags, and collapsing of multiple spans into single, ie
CODE
[font=arial][color=red][size=6]

becomes
CODE
<span style="font-family: arial; color: red; font-size: 6;">

instead of
CODE
<span style="font-family: arial;"><span style="color: red;"><span style="font-size: 6;">
Brendon Koz
Would it not be more efficient for your coding to, say, allow commas in a font control BBCode tag? I don't think any forum (or otherwise) converts multiple BBCodes into a single HTML tag.

If you still want it, I could possibly take a look at it Tuesday (since I'm not all that great either), but by then I would assume someone else would have figured it out.
Stephen
Got it all working, thanks anyway
Brendon Koz
Such an innovator. original.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.