问题
I have some strings in the database like SNEAKERS "SUPER STAR" GOLDEN GOOSE
. These are the titles for some products. When I output it normally inside a <p>
it shows the quotes, but when I echo it inside an input value, <input type="text" value="<?= $product->title ?">
the string gets truncated before the first double quote, so the value becomes just SNEAKERS
.
Is there a way I can output the double quotes inside the value of an input ?
EDIT: The closing tag was a typo, in the code it is closed.
回答1:
Use htmlspecialchars like so:
htmlspecialchars($product->title);
i.e
<input type="text" value="<?= htmlspecialchars($product->title) ?>">
回答2:
Evaluate this html, I think you'll see where the problem lies:
<input type="text" value="SNEAKERS "SUPER STAR" GOLDEN GOOSE">
If you look closely, you'll see that the double quotes from the string are closing the double quotes for the input. The solution to this as others have pointed out is to call htmlspecialchars
and pass it the string prior to outputting it.
You're also missing the ending >
for the closing PHP tag.
回答3:
You're missing your closing tag try this:
<input type="text" value="<?= $product->title ?>">
Also you need to escapte the double quotes inside the html (as in Wayne answer)
来源:https://stackoverflow.com/questions/25164007/php-double-quotes-dont-show-in-input-value-but-are-shown-in-normal-echo