I'm having a little bit of trouble making a sticky form that will remember what is entered in it on form submission if the value has double quotes. The problem is that the HTML is supposed to read something like:
<input type="text" name="something" value="Whatever value you entered" />
However, if the phrase: "How do I do this?" is typed in with quotes, the resulting HTML is similar to:
<input type="text" this?="" do="" i="" how="" value="" name="something"/>
How would I have to filter the double quotes? I've tried it with magic quotes on and off, I've used stripslashes and addslashes, but so far I haven't come across the right solution. What's the best way to get around this problem for PHP?
You want htmlentities().
<input type="text" value="<?php echo htmlentities($myValue); ?>">
The above will encode all sorts of characters that have html entity code. I prefer to use:
htmlspecialchars($myValue, ENT_QUOTES, 'utf-8');
This will only encode:
'&' (ampersand) becomes '&'
'"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
''' (single quote) becomes ''' only when ENT_QUOTES is set.
'<' (less than) becomes '<'
'>' (greater than) becomes '>'
You could also do a strip_tags on the $myValue to remove html and php tags.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With