Page 1 of 1

Idea needed for [quote] and [quote=xxx] replacement

Posted: Thu Dec 11, 2008 9:53 am
by godsquare
Hi All,

I'm try to create function to replace [ quote][ /quote] and [quote=xxx][/quote] to a table.

I've modify some function i found on the internet to look like

Code: Select all

function bbcode_format ($str) {
    $str = nl2br($str);
    $str = stripslashes($str);
 
    $simple_search = array(
                '/\[b\](.*?)\[\/b\]/is',                                
                '/\[i\](.*?)\[\/i\]/is',                                
                '/\[u\](.*?)\[\/u\]/is',
                '/\[url\=(.*?)\](.*?)\[\/url\]/is',                         
                '/\[url\](.*?)\[\/url\]/is',                             
                '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',    
                '/\[img\](.*?)\[\/img\]/is',                            
                '/\[font\=(.*?)\](.*?)\[\/font\]/is',                    
                '/\[size\=(.*?)\](.*?)\[\/size\]/is',                    
                '/\[color\=(.*?)\](.*?)\[\/color\]/is',        
                );
 
    $simple_replace = array(
                '<strong>$1</strong>',
                '<em>$1</em>',
                '<u>$1</u>',
                '<a href="$1">$2</a>',
                '<a href="$1">$1</a>',
                '<div style="text-align: $1;">$2</div>',
                '<img src="$1" />',
                '<span style="font-family: $1;">$2</span>',
                '<span style="font-size: $1'.'px;">$2</span>',
                '<span style="color: $1;">$2</span>',
                );
 
    // Do simple BBCode's
    $str = preg_replace ($simple_search, $simple_replace, $str);
 
    // Do <blockquote> BBCode
    $str = bbcode_quote ($str);
 
    return $str;
}
 
function bbcode_quote ($str) {
    $open = '<table border=1 cellspacing=0 cellpadding=10 align=left><tr><td style=\'border: 1px dotted\'>';
    $close = '</td></tr></table><br><br>';
 
    // How often is the open tag?
    preg_match_all ('/\[quote\]/i', $str, $matches);
    $opentags = count($matches['0']);
 
    // How often is the close tag?
    preg_match_all ('/\[\/quote\]/i', $str, $matches);
    $closetags = count($matches['0']);
 
    // Check how many tags have been unclosed
    // And add the unclosing tag at the end of the message
    $unclosed = $opentags - $closetags;
    for ($i = 0; $i < $unclosed; $i++) {
        $str .= $close;
    }
 
    // Do replacement
    $str = str_replace ('[' . 'quote]', $open, $str);
    $str = str_replace ('[/' . 'quote]', $close, $str);
 
    $str = htmlwrap($str);
 
    return $str;
}
This work fine if I have only [ quote][ /quote] but I have to idea how to replace [quote=xxx][/quote] to

Code: Select all

<p><strong>xxx Posted :</strong></p><br><table border=1 cellspacing=0 cellpadding=10><tr><td style=\'border: 1px dotted\'>[DATA]</td></tr></table><br>
Any suggestion will be really appreciate,

Re: Idea needed for [quote] and [quote=xxx] replacement

Posted: Thu Dec 11, 2008 3:56 pm
by infolock
Regular expressions is really my weakest point, but I can, however, explain what it should do...


find occurrance of [quote
find occurrance of either =url or ]
skip past string until [/quote]

a very small (and needs a lot of work) example might be /\[quote((=http.*\])|\]).*[/quote]\

this of course is not 100% sound, but you get the idea...


A simple way would just use explode imo..

Code: Select all

 
$a = explode("[quote", $string);
 
$what_is_after_quote = $a[1];
 
$url_or_brace = explode("]", $what_is_after_quote);
 
if( substr($url_or_brace[0], 0, 1) == "=") {
  $url = $url_or_brace[0];
}
$tmp = explode("[/quote]", $url_or_brace[1]);
$string = $tmp[1];
 
of course even this is very rough and needs work, but that's a second easier alternative... longer to execute, but easier to understand/fix...



And before I get flamed by anyone: A) I'm only giving a kick start with both examples. Im in a hurry to get out of here for a dinner party ;) b) I do not, in anyway, say that these examples work 100% out of the box, but again are merely examples of where to go.