如何使用 PHP 从输入框中去除引号?

发布于 2024-08-21 12:43:36 字数 335 浏览 9 评论 0原文

我有这样的:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开柳相依 2024-08-28 12:43:36

如果我正确理解了这个问题,您想从 $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.

例如:

echo '<input name="title" type="text" class="inputMedium" value="'
   . htmlspecialchars($inputData['title'])
   . '" />';

注意:根据您的情况(特别是关于您可能会使用的编码/字符集)使用),您可能会向 htmlspecialchars 传递一些附加参数。

一般来说,您应该始终对作为输出发送的数据进行转义,无论您使用哪种输出格式。

例如:

  • 如果您要生成一些 XML 或 HTML,则应该使用 htmlspecialchars
  • 如果如果您正在生成一些 SQL,则应该使用 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:

echo '<input name="title" type="text" class="inputMedium" value="'
   . htmlspecialchars($inputData['title'])
   . '" />';

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:

  • If you are generating some XML or HTML, you should use htmlspecialchars
  • If you are generating some SQL, you should use mysql_real_escape_string, or an equivalent, depending on the type of database you're working with
北恋 2024-08-28 12:43:36

用户输入应该通过 htmlspecialchars() 运行才能在这种情况下使用。

User input should be run through htmlspecialchars() to be used in this sort of case.

枕头说它不想醒 2024-08-28 12:43:36

我强烈建议您在显示用户在任何地方生成的任何内容之前使用 htmlentities($string, ENT_QUOTES) ...

I highly recommend you to use htmlentities($string, ENT_QUOTES) before displaying anything user generated anywhere...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文