Search code examples
phpforums

Quoting users in forums


I'm working on the comments section for a site, where users can quote something another user said. This is your basic "quote" button on a forum.

Using BBcode for this. But not sure how to accomplish the result.

How is this feature usually done?

i can have

[quote=username] some sentence [/quote]

which would ideally be converted to

<blockquote>username said:
some sentence
</blockquote>

As of now, i have a code that converts

"[quote=username] ... [/quote]"
 into
 <blockquote> ... </blockquote>

but i lose the username

this is the code i'm using

// output user comment
echo parse_quote( $row['user_comment'] );


// and this is the function to parse the quote

function parse_quote($str) {
    $str = preg_replace("/\[quote=[\w\s\-\W][^\]]{1,}\]/", "<blockquote>:", $str);  
    $str = preg_replace("/\[\/quote\]/", "</blockquote>", $str);
    return $str;
}

So in a nutshell, how is forums quoting usually done...is it the right way? If so, how can I convert

[quote=username] some sentence [/quote]

into

<blockquote>username said:
some sentence
</blockquote>

Solution

  • Try changing it to something like:

    function parse_quote($str) {
        $str = preg_replace("/\[quote=([^\]]+)\]/", "<blockquote>$1 said:", $str);  
        $str = preg_replace("/\[\/quote\]/", "</blockquote>", $str);
        return $str;
    }
    

    A little more modification will be required if you want to allow people to quote without specifying a username, like [quote]some text[/quote].