Amine Arous
Amine Arous

Reputation: 635

Text containing single quote as input value [PHP]

I try to show this string : «let's go» in a the value of an input tag

i wrote: $chaine="let's go";

echo "<input type=text name=test value='".$chaine."'>";

result: let

What can i do to show the correct string in this case ?

Upvotes: 4

Views: 3796

Answers (3)

Shakti Singh
Shakti Singh

Reputation: 86406

You can look at htmlentities()

htmlentities($chaine, ENT_QUOTES) ;

Upvotes: 4

bharath
bharath

Reputation: 1233

use htmlspecialchars

echo "<input type=text name=test value='".htmlspecialchars($chaine, ENT_QUOTES)."'>";

Upvotes: 8

KingCrunch
KingCrunch

Reputation: 131891

This produces

<input type=text name=test value='let's go'>

You can see, that for HTML (--> your Browser) the value ends after "let". Everything after that is invalid (but ignored). Escape

$chaine = "let\'s go";

However, in HTML double quotes are prefered and omitting the quotes is also no good style

$chaine="let's go";
echo '<input type="text" name="test" value="'.$chaine.'">';

In this case you must escape every double quote.

Upvotes: -1

Related Questions