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.

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题