add bbcode to the post help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

add bbcode to the post help

Post by gaogier »

Hey, I have made a post which gets data from the phpBB3 forum database.

The script only gets the data from 1 forum using the forum id.

The script works as smoothly as you could imagine, but there is a little problem, the script dose not use bbcode. I need the code to use bbcode, any ideas on how to help? I have had loads of people help on this, everyone has no idea.

Here is my code.

Code: Select all

<?php
 
 
 
//function displayNews($all = 0) {
//global $sitestyle;
    require_once ('../mysql_connect1.php');//connect to db
 
 
 
    if ($all == 0) {   
            $topic_query = "SELECT * FROM `phpbb_topics` WHERE `forum_id`= '63' ORDER BY `topic_time` DESC LIMIT 1"; // do topic query
    }else{
            $topic_query = "SELECT * FROM `phpbb_topics` WHERE `forum_id`= '63' ORDER BY `topic_time` DESC"; // do topic query
    }
        $topic_result = mysql_query ($topic_query);
            while ($topic_row = mysql_fetch_assoc($topic_result)) {
                $tt = $topic_row['topic_time'];
                $topic = $topic_row['topic_id'];
                $poster = $topic_row['topic_poster'];
                $first = $topic_row['topic_first_post_id'];
                $title = $topic_row['topic_title'];
                $topic_replies = $topic_row['topic_replies'];
                $topic_runescape = $topic_row['topic_runescape'];
                $post_query = "SELECT * FROM `phpbb_posts` WHERE  `topic_id` = '$topic' AND `poster_id` = '$poster' AND `post_id` = '$first'"; // do post subject query
                $post_result = mysql_query($post_query);
                while ($post_row = mysql_fetch_assoc($post_result)){
                    $post_id = $post_row['post_id'];
                }
                $use_query = "SELECT `username`, `user_avatar`, `user_email`, `user_rank` FROM `phpbb_users` WHERE `user_id` = '$poster'"; // do user query
                $use_result = mysql_query($use_query);
                while ($use_row = mysql_fetch_assoc($use_result)){
                     $user = $use_row['username'];
                     $user_avatar = $use_row['user_avatar'];
                     $user_email = $use_row['user_email'];
                     $rank = $use_row['user_rank'];                 
                }
                $news_query = "SELECT `post_text` FROM `phpbb_posts` WHERE `post_id`='$post_id'";
                $news_result = mysql_query($news_query);
                while ($news_row = mysql_fetch_assoc($news_result)){
                    $news = $news_row['post_text'];
                }
                $rank_result = mysql_query("SELECT `rank_title` FROM `phpbb_ranks` WHERE `rank_id`='$rank'");
                while($rank_row = mysql_fetch_assoc($rank_result)){
                    $ranks = $rank_row['rank_title'];
                }
                $date = date("D M jS, Y g:i a", $tt);
                if ($topic_replies == 0){
                $reply = '<a href="http://www.runehints.com/phpBB3/posting.php?mode=reply&f=63&t='.$topic.'">Nobody has commented on this piece of news yet. Be the first!</a>';
                $post_new ="";
                }else{
                $reply =  'comments(<a href="http://www.runehints.com/phpBB3/viewtopic.php?t='.$topic.'">'.$topic_replies.'</a>) |';
                $post_new = '<a href="http://www.runehints.com/phpBB3/posting.php?mode=reply&f=63&t='.$topic.'">Post a comment</a>';
                }
                if ($topic_runescape == 1){
                    $news1 = 'This news topic is taken straight from <a href="http://runescape.com">Runescape.com</a><br />_____________________________________________<br /><br />'.$news;
                }else{
                    $news1 = $news;
                }
                // Replace newlines //
                $news2 = str_replace("\n", "\n<br />\n", $news1);
        ?>
    
 
<ul class="list1"><li>
 
 
        <table width="100%" border="0">
          <tr>
            <td><center><a href="http://www.runehints.com/phpBB3/viewtopic.php?t=<?php echo $topic; ?>" class="news"><?php echo $title; ?></a> posted by <a href="http://www.runehints.com/phpBB3/memberlist.php?mode=viewprofile&u=<?php echo $poster; ?>"><?php echo $user; ?></a> on <?php echo $date;?></center> </td>
          </tr>
          <tr>
            <td><table width="100%" border="0">
              <tr>
                <td width="20%" valign="top"><center>
          
            <img src="<?php echo $user_avatar; ?>" ><br /><font class="small2"><?php echo $ranks; ?></font></center>
             </td>
                <td width="80%">        <p><font class="small2">
      <p><?php echo "$news2"; ?></p><br>
      <br /><center><font class="small2"><?php echo $reply. ' '. $post_new;?></font></center>
 
      
  </div><br /></td>
              </tr>
            </table></td>
          </tr>
        </table></li>
                                                                                                            </ul>
    <?php
    }
    
    /* if we aren't displaying all news, 
     * then give a link to do so */
    if ($all == 0) {
        echo "<center><a href=\"{$_SERVER['PHP_SELF']}" .
             "?action=all\">View all news</a></center>\n";
    }
    //mysql_close();
//}
 
?>
This is really important the bbcode is destroying my website. it needs to display bbcode as html code (or the basic link)


Thanks
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: add bbcode to the post help

Post by vargadanis »

This is quote simple as far as I understand BBCodes.
They are lightweight markup code used to make posts more secure so that spamming with HTML would become harder. What is does is storing the posts with bbcode in them and then when a post is loaded the boear replaces the BBCode to HTML.
So all you need to do is creating a capital insensitive string replacement on the content.
Most of the BBCodes can be found on either bbcode.org or on http://en.wikipedia.org/wiki/BBCode.
Just create 2 arrays in one of the the bbcodes in the other the HTML ones and do an str_replace on them.

Or if it is not that simple, let me know what is the BBCode looks like, the output and the expected output.
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: add bbcode to the post help

Post by gaogier »

i understand bbcode is secure and all that...

Just got got a clue on how to add it to my system...
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: add bbcode to the post help

Post by vargadanis »

gaogier wrote:i understand bbcode is secure and all that...

Just got got a clue on how to add it to my system...
So I got got a clue either/neither... The simple str_replace way would not work, would it?
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: add bbcode to the post help

Post by gaogier »

vargadanis wrote:
gaogier wrote:i understand bbcode is secure and all that...

Just got got a clue on how to add it to my system...
So I got got a clue either/neither... The simple str_replace way would not work, would it?
sorry meant to say, i havnt got a clue on how to add it to the system, i dont know php... i only know how to modify scripts slightly...

What is the str_replace way?
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: add bbcode to the post help

Post by vargadanis »

If I understand the code correctly you have lines from 40 to 43 to get the posts from the DB:

Code: Select all

while ($news_row = mysql_fetch_assoc($news_result)){
    $news = $news_row['post_text'];
}
For str_replace you need to have 2 arrays

Code: Select all

$array_search = array("[ b ]", "[ /b ]", "[ i ]", "[ /i ]");  // complete the list based on the URLS I gave you, DO NOT PUT white spaces after and before the [] signes
$array_replace = array("<b>", "</b>", "<i>", "</i>");
/*
* Now comes the part when we exchange the contents of array_search to array_replace
*/
$news = str_replace($array_search, $array_replace, $news_row['post_text']); // all BBcodes have been replaced to HTML
You need to put the arrays in front of the WHILE loop.
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: add bbcode to the post help

Post by gaogier »

the standered bold tags wont work...

What is the URL one? [ url = have something there? ]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: add bbcode to the post help

Post by RobertGonzalez »

You are hitting this on a box that runs phpBB aren't you? Just use the bbCode handler from the phpBB code.
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: add bbcode to the post help

Post by gaogier »

i would just need to copy this right?

Code: Select all

                case 3:
                    $this->bbcode_cache[$bbcode_id] = array(
                        'preg' => array(
                            '#\[url:$uid\]((.*?))\[/url:$uid\]#s'           => $this->bbcode_tpl('url', $bbcode_id),
                            '#\[url=([^\[]+?):$uid\](.*?)\[/url:$uid\]#s'   => $this->bbcode_tpl('url', $bbcode_id),
                        )
                    );
                break;
so the code would be like this?

Code: Select all

                     while ($news_row = mysql_fetch_assoc($news_result)){
            case 3:
                    $this->bbcode_cache[$bbcode_id] = array(
                        'preg' => array(
                            '#\[url:$uid\]((.*?))\[/url:$uid\]#s'           => $this->bbcode_tpl('url', $bbcode_id),
                            '#\[url=([^\[]+?):$uid\](.*?)\[/url:$uid\]#s'   => $this->bbcode_tpl('url', $bbcode_id),
                        )
                    );
                break;
$news = str_replace($array_search, $array_replace, $news_row['post_text']); // all BBcodes have been replaced to HTML
if so i get this error

Parse error: syntax error, unexpected T_CASE in /home/gaogier/public_html/stats.php on line 41
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: add bbcode to the post help

Post by RobertGonzalez »

No, phpBB's handling of bbCode parsing (and the runs/passes it makes) almost necessitate that you hook into the board to do this. There are bbCode ID that associate entries and a bunch of other things.
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: add bbcode to the post help

Post by gaogier »

i am not sure what you are getting at?
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: add bbcode to the post help

Post by vargadanis »

Everah is saying that in order to "decode" BBCodes to HTML you will have to digg into the coding of PHPbb and use it's own functions to do that job for you...
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: add bbcode to the post help

Post by gaogier »

ah, i see, how do i know what part of the code is? or even know where about to put the code? there is no tags saying anything...
Post Reply