dongliao9018 2013-01-03 22:37
浏览 10
已采纳

PHP简单的BB代码 - 结束格式化

I am trying to create a simple BB code like script in PHP for my forum. The problem is, I am not sure how to stop a user posting something like this..

[b] Hahah
[b] Will make all of your text
[b] on this page bold

This would display (because I'm replacing words)

<strong> Hahah
<strong> Will make all of your text
<strong> on this page bold

What would be a good way to prevent this and end the tags at the bottom of the post?

  • 写回答

1条回答 默认 最新

  • dongliangkeng1056 2013-01-03 22:47
    关注

    Replacing BBCode by HTML is actually not as easy as it seems. Just using str_replace() will never be good enough. Because, as you said, people can just only place the open tag and this way, change your layout. A popular 'solution' is to use preg_replace() instead, with a regular expression that matches both the open and close tag. That, however, is quite unsafe with BBCode tags like [url] (you can insert JavaScript in those URL tags).

    However, there are some libraries out there that are very good in replacing BBCode. StringParser_BBCode is one of those. Those libraries do a little more than just replacing. For example, they will usually prevent malformed BBCode like [b][i]foo[/b][i] to turn into <strong><em>foo</strong></em> (which is malformed HTML). And also, they can do a lot more on preventing XSS injections.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备