tags:

views:

116

answers:

3

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>
+2  A: 

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].

Jason Berry
+1  A: 

Well, one thing I'll suggest is that you want to avoid multiple passes through your file and PHP provides a fairly convenient way of doing that using preg_replace_callback():

function process_codes($str) {
  return preg_replace_callback('!\[(.+?)\]!', 'process_code', $str);
}

function process_code($matches) {
  if ($matches[1] == '/quote') {
    return '</blockquote>';
  } else if (preg_match('!quote\s*=\s*(.+?)!', $matches[1], $args)) {
    return "<blockquote>$args[1] said:<br><br>";
  }
  // etc
}
cletus
A: 

No, Forum generally uses the following format:

<div><strong>username</strong> said:</div>
<blockquote>
some sentence
</blockquote>
Lastnico