BB code analysis for PHPBB3

We are looking for the analysis of BB code in PHP using the functions of PHPBB3. I'm so far away:

<?php include_once("../../forum/includes/functions_content.php"); $text = "[b]bold text here[/b] not bold here"; $uid = $bitfield = $options = ''; echo("parsing"); echo generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true); echo("finished"); ?> 

However, this is echos parsing , but does not continue after that. I expect the output to look like this:

 <b>bold text here</b> not bold here 

Any help is much appreciated!

Edit

No answers work. I am looking for a standalone php page that turns a given line of BB code into an HTML line using the PHPBB3 BBCode parser.

+7
php bbcode phpbb3
source share
4 answers

The generation of bb codes is a two-step process. The first step (first pass)

generate_text_for_storage designed to store bbcode in the database, it is stored as bbcode, because you can modify bbcode without having to reprocess old messages.

Another feature you are looking for is

generate_text_for_display

PHPBB has wiki lists like these

https://wiki.phpbb.com/Tutorial.Parsing_text

https://wiki.phpbb.com/Generate_text_for_display

are the pages you are looking for.

Alternatively, you can use the bbcode direct class, code that also works

 $bbcode = new bbcode(base64_encode($bbcode_bitfield)); $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']); $post_text = smiley_text($post_text); $post_text = censor_text($post_text); 

You will need

 include($phpbb_root_path . 'includes/bbcode.' . $phpEx); include($phpbb_root_path . 'includes/functions_display.' . $phpEx); 

for last job

Full code for method 2 functions, with output

 <?php ini_set('display_errors', 1); define('IN_PHPBB', true); $phpbb_root_path = './forum/'; $phpEx = "php"; include($phpbb_root_path . 'common.' . $phpEx); include($phpbb_root_path . 'includes/bbcode.' . $phpEx); include($phpbb_root_path . 'includes/functions_display.' . $phpEx); // Start session management $user->session_begin(); $auth->acl($user->data); $user->setup('viewforum'); $text = "[b]bold text here[/b] not bold here"; $uid = $bitfield = $options = ''; echo("parsing"); echo generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true); var_dump($text); $text = generate_text_for_display($text, $uid, $bitfield, OPTION_FLAG_BBCODE ); var_dump($text); echo("finished"); 

What are the exits

 parsing string '[b:1vhn6cjx]bold text here[/b:1vhn6cjx] not bold here' (length=53) array (size=1) 0 => int 1 string '<span style="font-weight: bold">bold text here</span> not bold here' (length=67) finished 

Converting bb code is a two-step process to provide flexibility for the user and poster to customize the appearance of the message. First you need to process the text with the first function, and then process the second time to get html

+2
source share

To do this, you can use regular expressions to find BBCode tags and fix that between two tags.

Here is an example with bold text:

 $text = "[b]bold text here[/b] not bold here but still [b]bold here[/b]"; $pattern = '/\[b\](.*?)\[\/b\]/i'; $replacement = '<b>$1</b>'; echo preg_replace($pattern, $replacement, $text); 

Conclusion: <b>bold text here</b> not bold here but still <b>bold here</b> .

Additional information about preg_replace .

Can you notice the marker *? to make the record lazy and not greedy, thus working on multiple tags on the same line.

This regular expression will also work (after a little change) for italic text, underlined text. But you will have to write different ones for links, lists or images. You can find the list of BB code tags in wikipedia: BB code codes . On the same page, you will find sample HTML code for each type of tag that really helps you!

Now there is a library of PHP analysis PHP BBCode. This will save you a lot of time and will probably be more productive than using regular expressions.

Here are two examples of libraries: PECL and PEAR .

0
source share

This is how I got the working version based on your published code ...

1) Installed PHPBB3 on my local web server ... This: XAMPP, PHP 5.3.18 on Windows XP.

2) Checked all this, creating forums and sending messages as a "guest".

Everything is fine, so far ...

Then I edited the file 'index.php' to "include" all the standard "PHPBB3", but deleted all the displayed code.

Then I included your code and checked every step.

 <?php /** * * @package phpBB3 * @version $Id$ * @copyright (c) 2005 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ /** * @ignore */ define('IN_PHPBB', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './'; $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); include($phpbb_root_path . 'includes/functions_display.' . $phpEx); echo '<br />----------------------------------------------<br />'; echo "Hello from PHPBB3! Start<br />"; // show starting // note that multibyte support is enabled here $sampleText = "[b]bold text here[/b] not bold here"; var_dump($sampleText . ' : '. __FILE__ . __LINE__); $myNormalizeText = utf8_normalize_nfc($sampleText); var_dump($myNormalizeText .' : '. __FILE__. __LINE__); // variables to hold the parameters for submit_post $uid = $bitfield = $options = ''; echo("<br />parsing Start<br/>"); generate_text_for_storage($myNormalizeText, $uid, $bitfield, $options, true, true, true); var_dump($myNormalizeText .' :'. __FILE__. __LINE__); var_dump($uid .' :'. __FILE__. __LINE__); echo("<br />Parsing finished<br/>"); echo "<br />Goodbye from PHPBB3! END"; echo '<br />----------------------------------------------<br />'; ?> 

Output:

 ---------------------------------------------- Hello from PHPBB3! Start string '[b]bold text here[/b] not bold here : P:\developer\xampp\htdocs\phpBB3\index.php25' (length=82) string '[b]bold text here[/b] not bold here : P:\developer\xampp\htdocs\phpBB3\index.php33' (length=82) parsing Start string '[b:vkw79dbw]bold text here[/b:vkw79dbw] not bold here :P:\developer\xampp\htdocs\phpBB3\index.php41' (length=99) string 'vkw79dbw :P:\developer\xampp\htdocs\phpBB3\index.php42' (length=54) Parsing finished Goodbye from PHPBB3! END ------------------------ 

It seems that way.

0
source share

This code worked for me: Not 100% accurate, but it helps: Thanks https://gist.github.com/neo22s/2584465

with minor changes

 <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT post_id, post_text from phpbb_posts"; //adjust phpbb_ $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $converted_post_text=tohtml($row['post_text']); $post_id=$row['post_id']; $updated_sql="UPDATE phpbb_posts SET post_text='".$converted_post_text."' WHERE post_id='".$post_id."'"; $conn->query($updated_sql); echo $post_id . 'Done '. $converted_post_text; echo '<br>'; sleep(1); } } else { echo "0 results"; } $conn->close(); function tohtml($text,$advanced=true,$charset='utf-8'){ //special chars $text = htmlspecialchars($text, ENT_QUOTES,$charset); /** * This array contains the main static bbcode * @var array $basic_bbcode */ $basic_bbcode = array( '[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]', '[s]','[/s]', '[ul]','[/ul]', '[li]', '[/li]', '[ol]', '[/ol]', '[center]', '[/center]', '[left]', '[/left]', '[right]', '[/right]', ); /** * This array contains the main static bbcode html * @var array $basic_html */ $basic_html = array( '<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<s>', '</s>', '<ul>','</ul>', '<li>','</li>', '<ol>','</ol>', '<div style="text-align: center;">', '</div>', '<div style="text-align: left;">', '</div>', '<div style="text-align: right;">', '</div>', ); /** * * Parses basic bbcode, used str_replace since seems to be the fastest */ $text = str_replace($basic_bbcode, $basic_html, $text); //advanced BBCODE if ($advanced) { /** * This array contains the advanced static bbcode * @var array $advanced_bbcode */ $advanced_bbcode = array( '#\[color=([a-zA-Z]*|\#?[0-9a-fA-F]{6})](.+)\[/color\]#Usi', '#\[size=([0-9][0-9]?)](.+)\[/size\]#Usi', '#\[quote](\r\n)?(.+?)\[/quote]#si', '#\[quote=(.*?)](\r\n)?(.+?)\[/quote]#si', '#\[url](.+)\[/url]#Usi', '#\[url=(.+)](.+)\[/url\]#Usi', '#\[email]([\w\.\-] +@ [a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]*\.\w{1,4})\[/email]#Usi', '#\[email=([\w\.\-] +@ [a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]*\.\w{1,4})](.+)\[/email]#Usi', '#\[img](.+)\[/img]#Usi', '#\[img=(.+)](.+)\[/img]#Usi', '#\[code](\r\n)?(.+?)(\r\n)?\[/code]#si', '#\[youtube]http://[az]{0,3}.youtube.com/watch\?v=([0-9a-zA-Z]{1,11})\[/youtube]#Usi', '#\[youtube]([0-9a-zA-Z]{1,11})\[/youtube]#Usi' ); /** * This array contains the advanced static bbcode html * @var array $advanced_html */ $advanced_html = array( '<span style="color: $1">$2</span>', '<span style="font-size: $1px">$2</span>', "<div class=\"quote\"><span class=\"quoteby\">Disse:</span>\r\n$2</div>", "<div class=\"quote\"><span class=\"quoteby\">Disse <b>$1</b>:</span>\r\n$3</div>", '<a rel="nofollow" target="_blank" href="$1">$1</a>', '<a rel="nofollow" target="_blank" href="$1">$2</a>', '<a href="mailto: $1">$1</a>', '<a href="mailto: $1">$2</a>', '<img src="$1" alt="$1" />', '<img src="$1" alt="$2" />', '<div class="code">$2</div>', '<object type="application/x-shockwave-flash" style="width: 450px; height: 366px;" data="http://www.youtube.com/v/$1"><param name="movie" value="http://www.youtube.com/v/$1" /><param name="wmode" value="transparent" /></object>', '<object type="application/x-shockwave-flash" style="width: 450px; height: 366px;" data="http://www.youtube.com/v/$1"><param name="movie" value="http://www.youtube.com/v/$1" /><param name="wmode" value="transparent" /></object>' ); $text = preg_replace($advanced_bbcode, $advanced_html,$text); } //before return convert line breaks to HTML return $text; } ?> 
0
source share

All Articles