【问题标题】:PHP String Replace for BBCode [closed]BBCode的PHP字符串替换[关闭]
【发布时间】:2015-12-31 19:53:40
【问题描述】:

我想为我的论坛网站制作一个自定义 BBCode,但我遇到了一个问题,我很难修复它。

这是线程“[b]Bold[/b][i]Italic[/i][strike]Strike[/strike]”的主体的数据库中的内容。

但是输出显示如下“[i]Italic[/i][strike]Strike[/strike]”。

所以,我猜这是回显它的问题,但我不知道如何解决它。这是当前代码:

function bbcode($input) {
    $input = strip_tags($input);
    $input = htmlentities($input);

    $search = array('/\[b\](.*?)\[\/b\]/is');

    $replace = array('<b>$body</b>');

    return preg_replace($search, $preg_replace, $input);
}

while($row = mysql_fetch_array($threadquery, MYSQL_ASSOC)) {
    $body = str_replace("\n",'<br>', $row['body']);
}

echo bbcode($body);

【问题讨论】:

  • 为什么不省去麻烦并使用经过测试和信任的呢?我建议使用Parsedown
  • bbcode 有点过时了,只是嵌入了一个像cke editor这样的编辑器:ckeditor.com
  • @Anthony 请尝试我的库来解析短代码和 BBCode:github.com/thunderer/Shortcode。如果您需要更多信息,请提交问题,我会提供帮助。

标签: php regex bbcode


【解决方案1】:

正确的代码应该是:

$replace = array('<b>$1</b>');

return preg_replace($search, $replace, $input);

【讨论】:

  • 谢谢,它就像奇迹一样!
猜你喜欢
  • 2014-03-06
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多