Jump to content

Using Preg Replace to Parse bbCode Into a Function


ShoeLace1291

Recommended Posts

I am working on developing a custom forum with codeigniter.  I am almost completely finished with it, however, the only problem I am running into is parsing a bbcode quote tag.  I use two different arrays to supply to the preg_replace function that will replace the bbcodes with html.  The function I have written successfully replaces the

tag with the function, however, for some reason it does not pass the parameters correctly.  Any ideas?
 
<?php

function parse_bb($str){
        
        $str = nl2br($str);
        
        $find = array(
            "'\[b\](.*?)\[/b\]'is",
            "'\[i\](.*?)\[/i\]'is",
            "'\[u\](.*?)\[/u\]'is",
            "'\[s\](.*?)\[/s\]'is",
            "'\[img\](.*?)\[\/img\]'is",
            "'\[url\](.*?)\[/url\]'i",
            "'\[url=(.*?)\](.*?)\[/url\]'i",
            "'\[link\](.*?)\[/link\]'i",
            "'\[link=(.*?)\](.*?)\[/link\]'i",
            "'\[h1\](.*?)\[\/h1\]'is",
            "'\[h2\](.*?)\[\/h2\]'is",
            "'\[h3\](.*?)\[\/h3\]'is",
            "'\[ul\](.*?)\[\/ul\]'is",
            "'\[li\](.*?)\[\/li\]'is",
            "'\[p\](.*?)\[\/p\]'is",
            "'\[quote id=(.*?)\](.*?)\[\/quote\]'is"
        );
        
        $replace = array(
            '<strong>\1</strong>',
            '<em>\1</em>',
            '<u>\1</u>',
            '<s>\1</s>',
            '<img src="\1" \1alt="User Image" />',
            '<a href="\1">\1</a>',
            '<a href="\1">\2</a>',
            '<a href="\1">\1</a>',
            '<a href="\1">\2</a>',
            '<h1>\1</h1>',
            '<h2>\1</h2>',
            '<h3>\1</h3>',
            '<ul>\1</ul>',
            '<li>\1</li>',
            '<p>\1</p>',
            _parse_quote($post_id = "\1", $original_message = "\2")
        );

        $str = preg_replace($find, $replace, $str);
        
        
        
        return $str;
    
    }
    
function _parse_quote($post_id, $original_message){
        
        $CI =& get_instance();
        
        $CI->load->model('forums/message');
        
        $CI->message->get_info($post_id);
        
        if($CI->message->error == NULL){
                
                $message = $CI->message->info;
                
                $str = '
                        <blockquote>
                                <p>'.$original_message.'</p>
                        </blockquote>
                        ';
                        
        } else {
                
                $str = $CI->message->error;
                
        }
        
        return $str;
}
Edited by ShoeLace1291
Link to comment
Share on other sites

As it is now you're just executing _parse_quote() with the post ID and message being strings you really didn't intend for it to use. You have to do something more sophisticated that doesn't involve executing _parse_quote() until preg_replace() starts making the replacements.

 

Are you running PHP 5.3 or later?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.