phpBB

Development Wiki

Difference between revisions of "Tutorial.Parsing text"

From phpBB Development Wiki

(tabs don't appear to work, so I'll use spaces)
(Edits from Confluence)
 
(47 intermediate revisions by 15 users not shown)
Line 1: Line 1:
 +
== Database fields ==
 +
phpBB3 uses the following database fields where BBCode is allowed (forum description, forum rules, post content, ...):
 +
* '''foo''' - the text itself
 +
* '''foo_bbcode_uid''' - a randomly generated unique identifier to mark the bbcodes identified by the first pass encoding (which uses regular expressions) so they can be replaced using the quicker str_replace in the second pass.
 +
* '''foo_bbcode_bitfield''' - a [[wikipedia:Bit field|bit field]] containing the information which bbcode is used in the text so only the relavant ones need to be loaded from the database.
 +
* '''foo_options''' - a bit field containing the information whether bbcode, smilies and magic urls are enabled (OPTION_FLAG_BBCODE, OPTION_FLAG_SMILIES and OPTION_FLAG_LINKS). Sometimes you will find this separated into enable_bbcode, enable_smilies and enable_magic_url
 +
 +
column names vary from table to table.
 +
 
== Parsing text with BBCodes, smilies etc. ==
 
== Parsing text with BBCodes, smilies etc. ==
  
 
=== Inserting text to the DB ===
 
=== Inserting text to the DB ===
 
<php>
 
<php>
$text = utf8_normalize_nfc(request_var('text', '', true)); // will be modified by generate_text_for_storage (always use utf8_normalize_nfc when you retrieve text)
+
$text = utf8_normalize_nfc(request_var('text', '', true));
 +
$uid = $bitfield = $options = ''; // will be modified by generate_text_for_storage
 +
$allow_bbcode = $allow_urls = $allow_smilies = true;
 +
generate_text_for_storage($text, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
  
$uid = $bitfield = $flags = ''; // will be modified by generate_text_for_storage
+
$sql_ary = array(
 +
    'text'              => $text,
 +
    'bbcode_uid'        => $uid,
 +
    'bbcode_bitfield'  => $bitfield,
 +
    'bbcode_options'   => $options,
 +
);
  
$allow_bbcode = $allow_urls = $allow_smilies = true; // false is default for generate_text_for_storage function
+
$sql = 'INSERT INTO ' . YOUR_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
 +
$db->sql_query($sql);
 +
</php>
  
generate_text_for_storage($text, $uid, $bitfield, $flags, $allow_bbcode, $allow_urls, $allow_smilies);
+
The above method uses the bbcode options database field which is used in many places instead of enable_smiles, enable_bbcode and enable_magic_url
 +
 
 +
Here is how to insert it into the database using the enable_smilies, enable_bbcode and enable_magic_url tables
 +
<php>
 +
$text = utf8_normalize_nfc(request_var('text', '', true));
 +
$uid = $bitfield = $options = ''; // will be modified by generate_text_for_storage
 +
$allow_bbcode = $allow_urls = $allow_smilies = true;
 +
generate_text_for_storage($text, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
  
 
$sql_ary = array(
 
$sql_ary = array(
Line 15: Line 41:
 
     'bbcode_uid'        => $uid,
 
     'bbcode_uid'        => $uid,
 
     'bbcode_bitfield'  => $bitfield,
 
     'bbcode_bitfield'  => $bitfield,
     'bbcode_flags'     => $flags,
+
     'enable_bbcode'     => $allow_bbcode,
 +
    'enable_magic_url'  => $allow_urls,
 +
    'enable_smilies'    => $allow_smilies,
 
);
 
);
  
 
$sql = 'INSERT INTO ' . YOUR_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
 
$sql = 'INSERT INTO ' . YOUR_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
 
$db->sql_query($sql);
 
$db->sql_query($sql);
 +
 
</php>
 
</php>
 
text should be a TEXT column, bbcode_uid and bbcode_bitfield should be VARCHARs and bbcode_flags should be an INT column.
 
  
 
=== Displaying text downloaded from DB ===
 
=== Displaying text downloaded from DB ===
 +
This example uses the bbcode_options field which is used in forums and groups description parsing:
 
<php>
 
<php>
$sql = 'SELECT text, bbcode_uid, bbcode_bitfield, bbcode_flags FROM ' . YOUR_TABLE;
+
$sql = 'SELECT text, bbcode_uid, bbcode_bitfield, bbcode_options
$db->sql_query($sql);
+
    FROM ' . YOUR_TABLE;
 +
$result = $db->sql_query($sql);
 +
$row = $db->sql_fetchrow($result);
 +
$db->sql_freeresult($result);
  
$text = generate_text_for_display($row['text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_flags']);
+
$text = generate_text_for_display($row['text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']);
  
 
echo $text;
 
echo $text;
 +
</php>
 +
 +
 +
The next one uses the enable_bbcode, enable_smilies and enable_magic_url flags which can be used instead of the above method and is used in parsing posts:
 +
<php>
 +
$sql = 'SELECT text, bbcode_uid, bbcode_bitfield, enable_bbcode, enable_smilies, enable_magic_url
 +
    FROM ' . YOUR_TABLE;
 +
$result = $db->sql_query($sql);
 +
$row = $db->sql_fetchrow($result);
 +
$db->sql_freeresult($result);
 +
 +
$row['bbcode_options'] = (($row['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0) +
 +
    (($row['enable_smilies']) ? OPTION_FLAG_SMILIES : 0) +
 +
    (($row['enable_magic_url']) ? OPTION_FLAG_LINKS : 0);
 +
$text = generate_text_for_display($row['text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']);
 +
 +
echo $text;
 +
 
</php>
 
</php>
  
 
=== Generating text for editing ===
 
=== Generating text for editing ===
 
<php>
 
<php>
$sql = 'SELECT text, bbcode_uid FROM ' . YOUR_TABLE;
+
$sql = 'SELECT text, bbcode_uid
$db->sql_query($sql);
+
    FROM ' . YOUR_TABLE;
 +
$result = $db->sql_query_limit($sql, 1);
 +
$row = $db->sql_fetchrow($result);
 +
$db->sql_freeresult($result);
  
 
decode_message($row['text'], $row['bbcode_uid']);
 
decode_message($row['text'], $row['bbcode_uid']);
  
echo $row;
+
echo $row['text'];
 
</php>
 
</php>
 +
 +
=== The database options to use for the fields ===
 +
<highlightSyntax language="sqlnew">
 +
`bbcode_bitfield` varchar(255) collate utf8_bin NOT NULL default '',
 +
`bbcode_uid` varchar(8) collate utf8_bin NOT NULL,
 +
`bbcode_options` mediumint(4) collate utf8_bin NOT NULL,
 +
</highlightSyntax>
 
{{tutorial-stub}}
 
{{tutorial-stub}}
 +
<br>
 +
==See Also==
 +
* [[Strip_bbcode]]
 +
* [[Generate_text_for_display]]
 +
* [[Generate_text_for_storage]]
 +
<br>
 +
 +
[[Category:Tutorials]]
 +
[[Category:API]]
 +
[[Category:BBCode]]

Latest revision as of 14:29, 13 February 2011

Database fields

phpBB3 uses the following database fields where BBCode is allowed (forum description, forum rules, post content, ...):

  • foo - the text itself
  • foo_bbcode_uid - a randomly generated unique identifier to mark the bbcodes identified by the first pass encoding (which uses regular expressions) so they can be replaced using the quicker str_replace in the second pass.
  • foo_bbcode_bitfield - a bit field containing the information which bbcode is used in the text so only the relavant ones need to be loaded from the database.
  • foo_options - a bit field containing the information whether bbcode, smilies and magic urls are enabled (OPTION_FLAG_BBCODE, OPTION_FLAG_SMILIES and OPTION_FLAG_LINKS). Sometimes you will find this separated into enable_bbcode, enable_smilies and enable_magic_url

column names vary from table to table.

Parsing text with BBCodes, smilies etc.

Inserting text to the DB

$text utf8_normalize_nfc(request_var('text'''true));
$uid $bitfield $options ''// will be modified by generate_text_for_storage
$allow_bbcode $allow_urls $allow_smilies true;
generate_text_for_storage($text$uid$bitfield$options$allow_bbcode$allow_urls$allow_smilies);

$sql_ary = array(
    
'text'              => $text,
    
'bbcode_uid'        => $uid,
    
'bbcode_bitfield'   => $bitfield,
    
'bbcode_options'    => $options,
);

$sql 'INSERT INTO ' YOUR_TABLE ' ' $db->sql_build_array('INSERT'$sql_ary);
$db->sql_query($sql);

The above method uses the bbcode options database field which is used in many places instead of enable_smiles, enable_bbcode and enable_magic_url

Here is how to insert it into the database using the enable_smilies, enable_bbcode and enable_magic_url tables

$text utf8_normalize_nfc(request_var('text'''true));
$uid $bitfield $options ''// will be modified by generate_text_for_storage
$allow_bbcode $allow_urls $allow_smilies true;
generate_text_for_storage($text$uid$bitfield$options$allow_bbcode$allow_urls$allow_smilies);

$sql_ary = array(
    
'text'              => $text,
    
'bbcode_uid'        => $uid,
    
'bbcode_bitfield'   => $bitfield,
    
'enable_bbcode'     => $allow_bbcode,
    
'enable_magic_url'  => $allow_urls,
    
'enable_smilies'    => $allow_smilies,
);

$sql 'INSERT INTO ' YOUR_TABLE ' ' $db->sql_build_array('INSERT'$sql_ary);
$db->sql_query($sql);

Displaying text downloaded from DB

This example uses the bbcode_options field which is used in forums and groups description parsing:

$sql 'SELECT text, bbcode_uid, bbcode_bitfield, bbcode_options
    FROM ' 
YOUR_TABLE;
$result $db->sql_query($sql);
$row $db->sql_fetchrow($result);
$db->sql_freeresult($result);

$text generate_text_for_display($row['text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']);

echo 
$text;


The next one uses the enable_bbcode, enable_smilies and enable_magic_url flags which can be used instead of the above method and is used in parsing posts:

$sql 'SELECT text, bbcode_uid, bbcode_bitfield, enable_bbcode, enable_smilies, enable_magic_url
    FROM ' 
YOUR_TABLE;
$result $db->sql_query($sql);
$row $db->sql_fetchrow($result);
$db->sql_freeresult($result);

$row['bbcode_options'] = (($row['enable_bbcode']) ? OPTION_FLAG_BBCODE 0) +
    ((
$row['enable_smilies']) ? OPTION_FLAG_SMILIES 0) + 
    ((
$row['enable_magic_url']) ? OPTION_FLAG_LINKS 0);
$text generate_text_for_display($row['text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']);

echo 
$text;

Generating text for editing

$sql 'SELECT text, bbcode_uid
    FROM ' 
YOUR_TABLE;
$result $db->sql_query_limit($sql1);
$row $db->sql_fetchrow($result);
$db->sql_freeresult($result);

decode_message($row['text'], $row['bbcode_uid']);

echo 
$row['text'];

The database options to use for the fields


`bbcode_bitfield` varchar(255) collate utf8_bin NOT NULL default '',
`bbcode_uid` varchar(8) collate utf8_bin NOT NULL,
`bbcode_options` mediumint(4) collate utf8_bin NOT NULL,


See Also