>我有一个用户可以编辑的表单。
表单的一部分是标题字段。
我获取以前的标题值并通过 PHP 将其插入到字段中:
<input id="title" value=".$previousValue.">
问题是,当标题中有" 时。
假设标题是:
test"
它只会在浏览器中显示以下内容:
test
在元素检查时,"是 所以 PHP 没有任何问题。
谁能告诉我如何让报价显示在浏览器上?
谢谢
您必须将引号替换为 html 实体,否则浏览器会认为您只是在尝试关闭 value 属性:
<input id="title" value="<?php echo str_replace('"', '"', $previousValue); ?>">
编辑:或者,正如其他人正确指出的那样,您可以使用快捷方式来捕获其他情况:
<input id="title" value="<?php echo htmlspecialchars($previousValue); ?>">
<input id="title" value="test""> <<<--- see the issue there?
你必须转义你的引号:
<input id="title" value="test"">
所以在 PHP 中:
<input id="title" value="<?php echo htmlspecialchars($previousValue)?>">
在输出到 html 时,应始终使用 htmlspecialchars($your_variable)
。这将对"
、>
等字符进行编码,这样它们就不会破坏html。