0

我写了一个类来解析 bbcode,但是当我使用“转义”(函数 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;

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

4

1 回答 1

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;

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

于 2011-12-27T18:47:49.887 回答