Parsing Text

How to parse BBCode and smilies.

Submitted by harmlessgoat22 on 19 Apr 2008, 14:12

Parsing Text
Parsing text for bbcodes and smilies in phpBB3 is surprisingly simple. Heres how.

Required SQL Columns
You have to have three columns for the parsing to work. Here are their types (check includes/db/db_tools.php to find what each of these things means. I'm making it in this form so it's non-db exclusive). There's also more info on this in the create_schema_files article.
Code: Select all
'bbcode_options'         => array('TINT:1', 7),
'bbcode_bitfield'         => array('VCHAR', 0),
'bbcode_uid'            => array('VCHAR:8', 0),


Generating Text For Storage
Code: Select all
$content_parse = utf8_normalize_nfc($content);
$uid = $bitfield = $options = '';
$allow_bbcode   = ($config['allow_bbcode']) ? true : false;
$allow_smilies   = ($config['allow_smilies']) ? true : false;
$allow_urls      = ($config['allow_post_links']) ? true : false;
generate_text_for_storage($content_parse, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);

Generate_text_for_storage() is quite simple. You put in a bunch of variables, and it alters them. So, for all of the items, make sure you put them as variables.
$content is the message full of bbcode and smilies. $uid, $bitfield, and $options are things that are used when putting the data in the database, and later decoding it. $allow_bbcode, $allow_smilies, and $allow_urls are all for whether you are allowing those items or not. You can take the $config value for it, or just make your own, or have an ACP option for it. Really up to you.

After that, insert it into the database, with bbcode_uid as $uid, bbcode_options as $options, and bbcode_bitfield as $bitfield.

Generate Text for Display
Code: Select all
$content generate_text_for_display($content$uid$bitfield$options);   

That's pretty much it. $content is the content, $uid is bbcode_uid in the db, $bitfield is bbcode_bitfield, $options is bbcode_options. Pretty simple. You can just assign that to a template variable, and display it.

Decode Message
decode_message() is used to return the content to the stage it was when posted, so it is useful for editting.
So, if I had this:
Code: Select all
[b]hey[/b] yeh

It would get all scramblified with generate text for storage, then with decode_message() it would be returned back into it's "bbcode source" form.

Heres how it works:
Code: Select all
decode_message($row['content'], $row['bbcode_uid']);

You put in the content, you put in the uid. That's all there is to it.

Conclusion
Just like all my other articles end, if you need help with this, be sure to post in the forums here at phpBBmodders.net. If you think anything is unclear, feel free to contact me via PM, and I will try and update the article as soon as possible. Thank you.
 

License:

All articles in the knowledge base are licensed under the phpbbmodders beerware-nc license.

Back to category


Knowledge Base index