populating a textarea with special characters

2019-02-25 11:39发布

问题:

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;