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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.