phpBB

Development Wiki

Difference between revisions of "Tutorial.Parsing text"

From phpBB Development Wiki

(Inserting text to the DB)
(Displaying text downloaded from DB)
Line 22: Line 22:
  
 
=== 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_options
 
$sql = 'SELECT text, bbcode_uid, bbcode_bitfield, bbcode_options
Line 32: Line 33:
  
 
echo $text;
 
echo $text;
 +
</php>
 +
 +
This uses the old method which is used in 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);
 +
 +
$bbcode_options = (($row['enable_bbcode']) ? 1 : 0) + (($row['enable_smilies']) ? 2 : 0) + (($row['enable_magic_url']) ? 4 : 0);
 +
$text = generate_text_for_display($row['text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']);
 +
 +
echo $text;
 +
 
</php>
 
</php>
  

Revision as of 21:42, 5 February 2007

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);

text should be a TEXT column, bbcode_uid and bbcode_bitfield should be VARCHARs and bbcode_options should be an INT column.

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;

This uses the old method which is used in 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);

$bbcode_options = (($row['enable_bbcode']) ? 0) + (($row['enable_smilies']) ? 0) + (($row['enable_magic_url']) ? 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'];