I have a user form where i give the user the possibility to enter a text in a textarea and to change the text later.
Here is my example for the HTML textarea:
<textarea class="form-control" rows="4" name="answer_text" type="text">
<?php echo strip_tags(nl2br(htmlspecialchars($answers['answer_text']))); ?>
</textarea>
- Text Input:
"This is text"
. - Text Output (echo):
"This is text"
. - Desired / Expected output:
"This is text"
.
To avoid <br>
in the echo i use: strip_tags()
To show line breaks i use: nl2br()
For security reasons i use htmlspecialchars()
How ever, if the user uses quotation marks (" or "") in his text i get "
in my text echo. I can solve this by removing htmlspecialchars()
but i think its important to use it here. Is there any chance to get the correct output means the quotation marks shown instead of "
?
EDIT:
I was using two times htmlspecialchars()
. Once during getting the users input value by $_POST = htmlspecialchars($value)
to send it to the table and as second during the HTML echo as shown above.
As explained by @Cbroe the problem occurred because the data got encoded twice. Initial it " became "e; first - and then, during the second encoding, the & of that, got turned into &
.
Right way is in my case that i do not use htmlspecialchars()
during SQL INSERT
or UPDATE
(i am Using PDO prepared statements) and to only use htmlspecialchars()
during the HTML echo depending on the situation.