我的bbcode解析器类中htmlspecialchars的奇怪行为

时间:2011-12-27 18:23:49

标签: php bbcode htmlspecialchars

我编写了一个解析bbcode的类,但是当我使用“escape”时我遇到了问题(函数chtml :: encode是htmlspecialchars的包装器)。

MyBBcodeParser:http://snipt.org/srlo0

案例“BBcodeParser :: toHtml($ input,false)”:     Input: [b]hello[/b] <strong>hello2</strong>     Output: <strong>hello</strong> <strong>hello2</strong>(粗体应用)

案例“BBcodeParser :: toHtml($ input,true)”: Input: [b]hello[/b] <strong>hello2</strong> Output: &lt;strong&gt;hello&lt;/strong&gt;&amp;lt;strong&amp;gt;hello2&amp;lt;/strong&amp;gt;

我无法理解第二种情况的双重逃避......

1 个答案:

答案 0 :(得分:2)

如果您使用输入调用BBcodeParser::toHtml($input, true),则会返回以下内容:

<strong>hello</strong> &lt;strong&gt;hello2&lt;/strong&gt;

这是因为CHtml::encode在preg_replace之前应用,因此保留了从BBcode完整后生成的HTML代码,同时从输入中转出HTML代码(秒<strong> ,那个围绕hello2)。

现在,如果您再次将CHtml::encode应用于“已转义”的BBcode的结果,则会变得像您发布的那样(注意第一个强中的&lt;和第二个中的&amp;lt;) :

&lt;strong&gt;hello&lt;/strong&gt;&amp;lt;strong&amp;gt;hello2&amp;lt;/strong&amp;gt;

在第一种情况下,似乎根本没有编码。

相关问题