Parse BB code for PHPBB3
Asked Answered
R

4

8

Looking to parse BB code in PHP using PHPBB3's functions. I've got this far:

<?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 it echos parsing but doesn't continue after that. I'm expecting the output to be along the lines of:

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

Any help greatly appreciated!

Edit

No answers work still. I'm looking for a Standalone php page that turns given BB code string into an HTML string using PHPBB3's BBCode parser.

Regret answered 21/2, 2014 at 16:27 Comment(7)
try assigning generate_text_for_storage to a var so you can var_dump it. That might show you what happensHinkle
@Brainscrewer var_dump shows nothing (I assume it's failing on the function call)Regret
Temporarily add some var dump / echo lines in the function to see where and why it's failing?Lussi
You need to call generate_text_for_display() after calling *_storageStuck
@Stuck been trying this, no luck still. Anyone got a working example php file?Regret
Are you sure your file is correctly included (use resquire_once instead) ? Do you have a 500 error in console ? Can you see php logs ?Splanchnology
@TomGullen if you're willing to use 3rd party libraries, my own Shortcode may come in handy: github.com/thunderer/Shortcode .Gardener
B
2

Generating the bbcodes is a 2 step processes, Your doing the first step (first pass)

generate_text_for_storage is made for storing the bbcode in the database, Its stored as bbcode because you can change the bbcode without the need for reparsing old messages.

The other function your looking for is

generate_text_for_display

PHPBB has a wiki listing things like this

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

https://wiki.phpbb.com/Generate_text_for_display

are the pages your looking for.

Alternatively you can use the bbcode class direct, code which 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 the last one to work

Full code for the 2 functions method, 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");

Which outputs

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

bb code conversion is a 2 step process to give the flexibility for both user and poster to customize the view of the post. You will need to process the text first with the first function and then process a second time to get the html

Becket answered 26/2, 2014 at 15:9 Comment(3)
Changing to generate for display doesn't work either, also your alternative code also doesn't work for some reason! (I can't get any errors!)Regret
Put some working code in the answer with display errors so hopefully you can see more if yours doesnt workBecket
@TomGullen any luck with this one ?Becket
T
0

A way to do this yourself might be using regular expressions to find the BBCode tags and capture what's between two tags.

Here's 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);

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

More info on preg_replace.

You can notice the *? token to make the capture lazy and not greedy, thus working for multiple tags in the same string.

This regexp will also work (after a slight change) for italic text, underlined text. But you'll have to write a different one for links, lists or images. You can find the list of BB code tags on wikipedia : BB Code tags. On the same page, you'll find example HTML code for each kind of tag, which'll really help you !

Now, there are PHP BBCode parsing library. That'll save you a lot of time, and probably be more performant than using regexps.

Here are two examples of libraries: PECL and PEAR.

Turnage answered 24/2, 2014 at 15:58 Comment(2)
Thanks, but looking to use PHPBB's BB code parser so I don't need to write my own/keep it maintained!Regret
Also, if you did use this version, and I know it's only an example, we can exploit the img tagSip
F
0

Here is how i got a working version based on your posted code...

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

2) Checked it all worked by creating forums and posting messages as 'guest'.

All ok, so far...

I then edited my 'index.php' file to 'include' all the standard 'PHPBB3' stuff but removed all the display code.

I then 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 to do as asked.

Featherstitch answered 24/2, 2014 at 18:36 Comment(2)
Hi Ryan, it doesn't seem to work at all for me, also your solution doesn't seem to be putting HTML out?Regret
Oh, I even run the above code via the web server! It is the supplied 'index.php' file modified as above!Featherstitch
N
0

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

, with minor modifications

<?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's 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://[a-z]{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's 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;

    }

?>
Noenoel answered 5/10, 2016 at 1:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.