populating a textarea with special characters

我的梦境 提交于 2019-12-01 22:23:57

问题


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.

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.

Creating a HTML file with the following demonstrates my issue.

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

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.

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.

Thanks, Muchly


回答1:


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

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

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

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




回答2:


escape the & as &amp;



来源:https://stackoverflow.com/questions/4037263/populating-a-textarea-with-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!