Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input field containing double quotes value

Tags:

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?

like image 341
Testadmin Avatar asked Jan 05 '10 05:01

Testadmin


People also ask

How do you pass a string with double quotes?

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.

How do you put quotation marks in a string in HTML?

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.

What does 2 quotation marks mean in Java?

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.

How do you escape quotes in HTML?

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.


2 Answers

Use htmlentities:

<input value="<?php echo htmlentities($value);?>"> 
like image 174
Emil Vikström Avatar answered Oct 05 '22 15:10

Emil Vikström


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 &quot; inches" /> 
like image 31
Pascal MARTIN Avatar answered Oct 05 '22 16:10

Pascal MARTIN