Hi, I am trying to create a forum within which I have a "Post a Thread" page which has a simple HTML textarea element. I also have those BOLD and Itallic buttons that would wrap your text with UBB code when you click on them. As you know there are many UBB codes like , ,
I wrote 3 forum software, and I suggest as much as you save processing, you make the forum faster. The very first things is, I will suggest you not convert bbcode to html while saving, rather do the parsing when displaying. The bb code function you are using may be more resources consuming when large list of bbcodes added. Something like this; $search = array( '#\[url]([a-z]+?://){1}(.*?)\[/url]#', '#\[url](.*?)\[/url\]#', '#\[url=([a-z]+?://){1}(.*?)\](.*?)\[/url]#', '#\[b](.*?)\[/b\]#', '#\[i](.*?)\[/i\]#', '#\[u](.*?)\[/u\]#', '#\[img]([a-z]+?://){1}(.*?)\[/img]#', '#\[img](.*?)\[/img\]#' ) ; $replace = array( '<a href="\1\2" target=_blank>$1$2</a>', '<a href=http://\1\2 target=_blank>\1</a>', '<a href="\1\2" target=_blank>\3</a>', '<b>\1</b>', '<i>\1</i>', '<u>\1</u>', '<img src="\1\2">', '<img src="\1\2" />' ) ; $text = preg_replace($search, $replace, $text); PHP: I hope it helps. regards
Thanx Vooler Can you please post a complete list of UBB codes and their replacement so that I dont miss any other? Also, i m a newbie in regular expression so it would help me if you post it. Thanx
Reply to your private message, as I noticed you cant receive private message, so here you go. LiteBoards.com was the site used to be a site for free forum software. But is abandoned now, but I can send you software zip. this was my first forum software using plain files not database. Then I wrote LitBoard Laser with MySQL, but is property of clixint technologies and they use on their forums, I cant disclose its source. The third one I wrote was Flash based, and probably you dont need that one. Please let me know in the thread what you need. regards
Don't change to HTML when inserting. If you do, you will mix HTML and BBCODE. For example, if someone quotes a BBCODE lesson, the above won't work. Just parse the BBCODE when displaying it (VB/PHPBB/etc..) all do it that way. Here is my BBCode function: // BBCode Format function bbcode_format ($comment_body) { $comment_body = htmlentities($comment_body); $simple_search = array( '/\[b\](.*?)\[\/b\]/is', // Bold '/\[i\](.*?)\[\/i\]/is', // Italic '/\[u\](.*?)\[\/u\]/is', // Underline '/\[url\=(.*?)\](.*?)\[\/url\]/is', // URL + Title '/\[url\](.*?)\[\/url\]/is', // URL '/\[align\=(left|center|right)\](.*?)\[\/align\]/is', // Alignment '/\[img\](.*?)\[\/img\]/is', // Image '/\[mail\=(.*?)\](.*?)\[\/mail\]/is', // MailTo + Title '/\[mail\](.*?)\[\/mail\]/is', // MailTo '/\[font\=(.*?)\](.*?)\[\/font\]/is', // Font '/\[size\=(.*?)\](.*?)\[\/size\]/is', // Size '/\[color\=(.*?)\](.*?)\[\/color\]/is', // Color ); $simple_replace = array( '<strong>$1</strong>', '<em>$1</em>', '<u>$1</u>', '<a href="$1">$2</a>', '<a href="$1">$1</a>', '<div style="text-align: $1;">$2</div>', '<img src="$1" />', '<a href="mailto:$1">$2</a>', '<a href="mailto:$1">$1</a>', '<span style="font-family: $1;">$2</span>', '<span style="font-size: $1;">$2</span>', '<span style="color: $1;">$2</span>', ); // Do simple BBCode's $comment_body = preg_replace ($simple_search, $simple_replace, $comment_body); // Do <blockquote> BBCode $comment_body = bbcode_quote ($comment_body); return $comment_body; } // BBCode Quote Format function bbcode_quote ($comment_body) { $open = '<blockquote>'; $close = '</blockquote>'; // How often is the open tag? preg_match_all ('/\[quote\]/i', $comment_body, $matches); $opentags = count($matches['0']); // How often is the close tag? preg_match_all ('/\[\/quote\]/i', $comment_body, $matches); $closetags = count($matches['0']); // Check how many tags have been unclosed // And add the unclosing tag at the end of the message $unclosed = $opentags - $closetags; for ($i = 0; $i < $unclosed; $i++) { $comment_body .= '</blockquote>'; } // Do replacement $comment_body = str_replace ('[' . 'quote]', $open, $comment_body); $comment_body = str_replace ('[/' . 'quote]', $close, $comment_body); return $comment_body; } PHP: Peace,
Was wondering if u could write me a function in php that would convert the following to HTML and another function that would convert html to ubbc? To avoid problems, I have replaced [ with < <b]</b] <i]</i] <u]</u] <s]</s] <glow=red,2,300]</glow] <shadow=red,left,300]</shadow] <move]</move] <pre]</pre] <left]</left] <center]</center] <right]</right] <hr] <size=2]</size] <font=Verdana]</font] <blockquote]</blockquote] <url]</url] <ftp]</ftp] <img]</img] <email]</email] <table]<tr]<td]</td]</tr]</table] <tr]</tr] <td]</td] <sup]</sup] <sub]</sub] <tt]</tt] <code]</code] <quote]</quote] <list] <*] <*] <*] </list] Code (markup):
HI First of all thanx for your code. Was wondering if you could add the following in your ubb code, then it would really be helpful for me. http://cupidsystems.com/ubbc_replacement.txt Thanx so much