用特殊字符填充文本区域 [英] populating a textarea with special characters

查看:152
本文介绍了用特殊字符填充文本区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用用户的先前输入来填充文本区域.这是从数据库中提取的,并设置为textarea服务器端的内容.

I'm populating a textarea with previous input of a user. This is pulled from a database and set as the content of the textarea server side.

似乎我们遇到了错字和特殊字符组合的问题.如果用户最初输入的是&#6,则当我尝试用该文本区域填充文本区域时,它仅呈现一个小方块,就像它解释字符编码值一样.

It seems we are having an issue with a typo and a combination of special characters. if the user inputs &#6 originally, when I try to populate my textarea with that it just renders a little square like its interpreting the character encoded value.

使用以下内容创建HTML文件演示了我的问题.

Creating a HTML file with the following demonstrates my issue.

<textarea name"mytextarea">some text &#5 some more text </textarea

这是一个错字,用户打算输入#5& #6 因此,解决此问题的方法只是确保用户在输入&"号时在其两边都留有空格,然后再在文本区域中显示它.从我所用到看到的,这只是一个特殊的字符问题.

this is a typo, the user intended to enter #5 & #6 so a fix for this is simply to ensure when the user puts an ampersand in that I have a space on either side of it before I display it in the textarea. Its just a special character issue backwards from what i'm use to seeing.

我很好奇是否有一种方法可以使文本区域像用户键入的字符一样显示字符,并通过表单提交保留该区域.为了节省在将文本放入文本区域之前必须对文本进行解析或html编码的麻烦.

I'm curious if there is a way to get the text area to display the characters like the user typed it and preserve that through form submission. To save the over head of having to parse or html encode the text before putting into the textarea.

谢谢,

推荐答案

textarea内,您需要将以下字符转换为其HTML实体:

Inside a textarea, you need to convert the following characters into their HTML entities:

& => &amp;
> => &gt;
< => &lt;

这样,&#5将变为&amp;#5.在视觉上,对用户而言,它将保留为&#5.

That way, &#5 would become &amp;#5. Visually, to the user, it would remain &#5.

您没有指定正在使用的服务器端语言.在PHP中,正确的功能应为 htmlspecialchars()

You are not specifying the server side language you're using. In PHP, the correct function would be htmlspecialchars()

这篇关于用特殊字符填充文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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