In my PHP project I have a value containing special characters like ",', etc. (" 5 " inches, '3.5' inches, etc.). But it does not appear in a text field. How can I display this?
Is it possible to display this value in a text box?
If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.
The <q> HTML element indicates that the enclosed text is a short inline quotation. Most modern browsers implement this by surrounding the text in quotation marks.
Within a character string, to represent a single quotation mark or apostrophe, use two single quotation marks. (In other words, a single quotation mark is the escape character for a single quotation mark.) A double quotation mark does not need an escape character.
Adding these to the database is easy, escape them with " / ' etc. Nicely enough if you put " in the value clause of an input, it displays " on the screen as you want it to. Single quotes are a doddle, they can be as is if need be as their within doubles.
Use htmlentities:
<input value="<?php echo htmlentities($value);?>">
I suppose your "text box" is an HTML <input>
element?
If so, you are displaying it using something like this:
echo '<input name="..." value="' . $yourValue . '" />';
If it's the case, you need to escape the HTML that's contained in your variable, with htmlspecialchars
:
echo '<input name="..." value="' . htmlspecialchars($yourValue) . '" />';
Note that you might have to add a couple of parameters, especially to specify the encoding your are using.
This way, considering $yourValue
has been initialized like this :
$yourValue = '5 " inches';
You'll get from this generated HTML:
<input name="..." value="5 " inches" />
To that one, which works much better:
<input name="..." value="5 " inches" />
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