在 PHP 中处理所见即所得数据
我需要使用所见即所得编辑器来处理用户输入。
你如何在 php 中处理这个?
如果我检索数据并使用 htmlspecialchars ,那么所有由所见即所得编辑器转换为特殊字符的字符都会变得混乱。
例如,引用将是 "e;
当我在 php 中使用 htmlspecialchars
时, &会被转换为 &
这将是一个明显的问题。有什么想法吗?
I need to use a wysiwyg editor for handling user input.
How do you process this in php?
If I retrieve the data and use htmlspecialchars then all the characters that were converted to special characters by the wysiwyg editor will be messed up.
For example quote will be "e;
When I use htmlspecialchars
in php the & will be converted to &
It will be an obvious problem. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过保留正在修改的内容的纯文本和附加 HTML 记录?您可以显示纯文本,当您保存它时,您也可以将其转换为 html 并将其保存在单独的字段中?
如果特殊字符被转换为 HTML,那么当您将文本打印到 html 中的可编辑表单字段时,它们是否仍然会正确显示(对用户)?
如果我误解了请告诉我
Have you considered keeping a plain-text and an additional HTML record of whatever is being modified? You can display the plaintext and when you save it you could convert it to html also and save that in a seperate field?
If special chars are being converted to HTML though, wouldn't they still appear properly (to the user) when you are printing text out to editable form fields in html?
Let me know if I've misunderstood
大多数编辑器(CKEditor、CLEditor 和 NicEdit 等)支持两种输入模式:可视输入和直接输入(通常称为 HTML 模式)。
当用户在可视模式下输入文本时,编辑器会在用户键入其内容时将类似 html 的字符转换为相应的 HTML 实体。在这种模式下,编辑器通常会为用户添加标记(主要是段落)。
直接输入就像您期望的名称一样;用户会接触到组成其内容的 HTML。
如何处理输入数据主要取决于用户角色。
如果用户是可信的(即公司网站的管理员),则用户应该能够使用两种输入模式。
如果用户不受信任(匿名用户在博客文章上发表评论),则用户不应该能够输入(可能是恶意的,想想XSS) 标记。
如果您的用户需要一些选项来格式化其内容,您可能应该考虑使用其他类型的标记,例如 BBCode< /a>.这可以防止用户将任何
标签注入可能向其他用户显示的内容中。
不过,您仍然需要从用户内容中删除所有 HTML 标签。
Most editors (CKEditor, CLEditor and NicEdit to mention a few) supports two modes of input: Visual and direct input (usually called HTML mode).
When the user is entering text in visual mode, the editor takes care of converting html-like characters to the respective HTML entity while the user is typing his/her content. In this mode, the editor will typically add markup for the user (mostly paragraphs).
Direct input works like you'd expect from the name; The user is exposed to the HTML his or her content is made up of.
How you should handle the input data depends mostly on the users role.
If the user is trusted (i.e. an administrator for a company website), the user should be able to use both input modes.
If the user is untrusted (an anonymous user posting a comment on a blog post), the user should not be able to input (potentially malicious, think XSS) markup.
If your users needs some options for formatting their content, you should probably look into using another type of markup, e.g BBCode. This prevents the user from injecting any
<script>
tags into the content that might be shown to other users.You will still need to strip any HTML tags from the user content though.