phpBB

Development Wiki

Difference between revisions of "Tutorial.Parsing text"

From phpBB Development Wiki

Line 3: Line 3:
 
=== Inserting text to the DB ===
 
=== Inserting text to the DB ===
 
<php>
 
<php>
// $text will be modified by generate_text_for_storage
+
$text = utf8_normalize_nfc(request_var('text', '', true));
// always use utf8_normalize_nfc when you retrieve a multibyte string request variable
+
$text = utf8_normalize_nfc(request_var('text', '', true));  
+
 
+
 
$uid = $bitfield = $flags = ''; // will be modified by generate_text_for_storage
 
$uid = $bitfield = $flags = ''; // will be modified by generate_text_for_storage
 
+
$allow_bbcode = $allow_urls = $allow_smilies = true;
$allow_bbcode = $allow_urls = $allow_smilies = true; // false is default for generate_text_for_storage function
+
 
+
 
generate_text_for_storage($text, $uid, $bitfield, $flags, $allow_bbcode, $allow_urls, $allow_smilies);
 
generate_text_for_storage($text, $uid, $bitfield, $flags, $allow_bbcode, $allow_urls, $allow_smilies);
  
Line 28: Line 23:
 
=== Displaying text downloaded from DB ===
 
=== Displaying text downloaded from DB ===
 
<php>
 
<php>
$sql = 'SELECT text, bbcode_uid, bbcode_bitfield, bbcode_flags
+
$sql = 'SELECT text, bbcode_uid, bbcode_bitfield, bbcode_options
 
     FROM ' . YOUR_TABLE;
 
     FROM ' . YOUR_TABLE;
$result = $db->sql_query_limit($sql, 1);
+
$result = $db->sql_query($sql);
 
$row = $db->sql_fetchrow($result);
 
$row = $db->sql_fetchrow($result);
 
$db->sql_freeresult($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;
Line 49: Line 44:
 
decode_message($row['text'], $row['bbcode_uid']);
 
decode_message($row['text'], $row['bbcode_uid']);
  
echo $row;
+
echo $row['text'];
 
</php>
 
</php>
 
{{tutorial-stub}}
 
{{tutorial-stub}}

Revision as of 15:55, 4 February 2007

Parsing text with BBCodes, smilies etc.

Inserting text to the DB

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

$sql_ary = array(
    
'text'              => $text,
    
'bbcode_uid'        => $uid,
    
'bbcode_bitfield'   => $bitfield,
    
'bbcode_flags'      => $flags,
);

$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_flags should be an INT column.

Displaying text downloaded from DB

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

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'];