跳过解析CODE标记内的BBCode [英] Skip parsing the BBCode inside the CODE tag

查看:82
本文介绍了跳过解析CODE标记内的BBCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用regex解析BBCode以便将其替换为HTML.由于[code]标签解析,我现在陷入困境.

I'm parsing the BBCode using the regex in order to replace it into the HTML. I'm stuck right now because of the [code] tags parsing.

基本上,当您执行[code][b]this is bb[/b] [u]code in[/u] [i]code[/i][/code]时,不应替换[code]标记内的[b][u][i]和类似标记.

Basically, when you do [code][b]this is bb[/b] [u]code in[/u] [i]code[/i][/code] it shouldnt replace the [b], [u], [i] and similar tags that are INSIDE of the [code] tag.

不幸的是,使用了preg_replace:

Unfortunately, using the preg_replace:

$this->_text = preg_replace('/\[i](.+?)\[\/i]/i', '<em>\1</em>', $this->_text);
$this->_text = preg_replace('/\[code](.+?)\[\/code]]/i', '<code>\1</code>', $this->_text);

将导致全部替换,并且[code]标记内的代码将变为HTML格式.

will cause replacing them all, and the code inside of the [code] tag will become HTML formated.

有什么解决方法吗?我需要主意.我本来是想在解析其余部分之前在[code]中转义[]括号中的字符,但是这个想法听起来很愚蠢.

Is there any work-around for this? I need ideas. I was thinking about escaping the [, ] brackets characters in [code] before parsing the rest, but this idea sound silly.

推荐答案

如果要从外部进行解析,则代码标签需要这样的

If you are going to parse from the exterior in, you need something like this for your code tag

(?:\[code])(.*)(?=(?:\[\/code]))

但是,正如其他人提到的那样,您在执行此操作时应该非常小心,因为您不能保证会得到什么,它可能格式错误,然后最终会变成一团糟的标签,甚至是不完整的混合物解析和未解析的bbcode.

But as other have mentioned, you should be very careful with you do with this because you can not guarantee what is coming to you, it may be malformed and then you will end up with a mess of tags or even an incomplete mixture of parsed and unparsed bbcodes.

如果您有两个[/code] [/code],即使我刚刚编写的代码也会失败.

Even the code that i just wrote will fail if you have two [/code][/code].

这篇关于跳过解析CODE标记内的BBCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆