Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Using Preg Replace to Parse bbCode Into a Function

$
0
0

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 [quote] 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;
}

Viewing all articles
Browse latest Browse all 13200

Trending Articles