我想要实现的是使用BBCode的合理方法,但是通过htmlentities()解析所有其他数据。我认为这应该是可能的,我正在考虑围绕[]符号爆炸的思路,但我认为可能有更好的方法。
有什么想法吗?
答案 0 :(得分:0)
htmlentities()
无法解析。相反,它编码数据,因此可以安全地显示在HTML文档中。
您的代码如下所示:
htmlentities
在数据结构中转义纯文本。您的渲染功能将是递归的。一些指定关系的伪函数:
render( x : plain text ) = htmlentities(x)
render( x : bold tag ) = "<b>" . render( get_contents_of ( x )) . "</b>"
render( x : quote tag ) = "<blockquote>" .
render( get_contents_of( x )) .
"</blockquote>"
...
render( x : anything else) = "<b>Invalid tag!</b>"
所以你看,htmlentities
只有在你将输出呈现给HTML时才会发挥作用,所以如果你的纯文本应该包含特殊字符,例如&lt;和&gt;。如果您要渲染为纯文本,则根本不会使用函数调用。