引用论坛中的用户

发布于 2024-08-03 03:18:58 字数 1054 浏览 6 评论 0原文

我正在一个网站的评论部分工作,用户可以在其中引用其他用户所说的内容。这是论坛上的基本“引用”按钮。

为此使用 BBcode。但不确定如何实现结果。

这个功能通常是如何实现的呢?

我可以拥有

[quote=username] some sentence [/quote]

理想情况下可以转换为的

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

代码到目前为止,我有一个可以转换的代码

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

,但我丢失了用户名,

这是我正在使用的代码

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

所以简而言之,论坛引用通常是如何完成的......这是正确的吗方式?如果是这样,我怎样才能转换

[quote=username] some sentence [/quote]

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巷雨优美回忆 2024-08-10 03:18:58

尝试将其更改为以下内容:

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

如果您希望允许人们在不指定用户名的情况下引用,例如[quote]一些文本[/quote],则需要进行更多修改。

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

枫以 2024-08-10 03:18:58

好吧,我建议的一件事是,您希望避免多次传递文件,PHP 提供了一种相当方便的方法,使用 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
}

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
}
紅太極 2024-08-10 03:18:58

不是,论坛一般采用以下格式:

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

No, Forum generally uses the following format:

<div><strong>username</strong> said:</div>
<blockquote>
some sentence
</blockquote>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文