如何使用 PHP 从输入框中去除引号?
我有这样的:
<input name="title" type="text" class="inputMedium" value="' . $inputData['title'] . '" />
我想从用户输入中删除引号,这样如果有人输入如下内容: “这是我的标题”它不会弄乱我的代码。
我尝试了这个,但它不起作用:
$inputData['title'] = str_replace('"', '', $_POST['title']);
I have this:
<input name="title" type="text" class="inputMedium" value="' . $inputData['title'] . '" />
I want to strip quotes from user input so that if someone enters something like:
"This is my title" it wont mess up my code.
I tried this and it's not working:
$inputData['title'] = str_replace('"', '', $_POST['title']);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确理解了这个问题,您想从
$inputData['title']
中删除"
,这样您的 HTML 代码就不会混乱?如果是这样,“正确的" 解决方案不是删除双引号,而是在执行实际输出之前转义它们。
Considering you are generating HTML, **you should use the [`htmlspecialchars`][1] function**; this way, double-quotes *(and a couple of other characters)* will be encoded to HTML entities, and will not cause any trouble when injected into your HTML markup.
例如:
注意:根据您的情况(特别是关于您可能会使用的编码/字符集)使用),您可能会向
htmlspecialchars
传递一些附加参数。一般来说,您应该始终对作为输出发送的数据进行转义,无论您使用哪种输出格式。
例如:
htmlspecialchars
mysql_real_escape_string
或等效项,具体取决于您正在使用的数据库类型If I understand the question correctly, you want to remove
"
from$inputData['title']
, so your HTML code is not messed up?If so, the "right" solution is not to remove double-quotes, but to escape them before doing the actual output.
Considering you are generating HTML, **you should use the [`htmlspecialchars`][1] function**; this way, double-quotes *(and a couple of other characters)* will be encoded to HTML entities, and will not cause any trouble when injected into your HTML markup.
For instance:
Note: depending on your situation (especially, about the encoding/charset you might be using), you might to pass some additional parameters to
htmlspecialchars
.Generally speaking, you should always escape the data you are sending as an output, not matter what kind of output format you have.
For instance:
htmlspecialchars
mysql_real_escape_string
, or an equivalent, depending on the type of database you're working with用户输入应该通过
htmlspecialchars()
运行才能在这种情况下使用。User input should be run through
htmlspecialchars()
to be used in this sort of case.我强烈建议您在显示用户在任何地方生成的任何内容之前使用 htmlentities($string, ENT_QUOTES) ...
I highly recommend you to use htmlentities($string, ENT_QUOTES) before displaying anything user generated anywhere...