【问题标题】:input field showing an URL with quotes显示带有引号的 URL 的输入字段
【发布时间】:2015-03-01 09:31:50
【问题描述】:

我必须将一些 Wikipedia URL 存储到 MariaDB 数据库中。

碰巧有些网址包含引号,例如:

https://en.wikipedia.org/wiki/%22Heroes%22

所以我使用urlencode() 将它们存储为"en.wikipedia.org%2Fwiki%2F%22Heroes%22"

如果我 urldecode() 将 URL 显示在 <input type="text"> 字段中而没有所有 %(它们会吓到不熟练的用户),则引号会破坏 input 值。

我发现这种解决方法可以更舒适地显示结果:

$url = 'en.wikipedia.org%2Fwiki%2F%22Heroes%22'; // it comes in this way from the DB
$tmp = str_replace('%22','"', $url);
$url_input = urldecode($tmp);
echo "<input type=\"text\" value=\"$url_input\" />";

$url_input 的值作为&lt;a href 锚点工作顺利,然后来自表单的查询使用FILTER_SANITIZE_URLurlencode() 过滤以将其存储在数据库中。

有没有更好的方法来做到这一点?

【问题讨论】:

  • 你可以在 HTML 中使用单引号 -> value='$url_input' 但是你可能想确保 url 也不包含单引号?
  • 喜欢玛莎葡萄园岛?
  • 是的,就像那样。这就是为什么人们应该避免在 URL 中使用引号,维基百科只是没有收到消息,并且用双引号替换单引号也不是一种选择,因为 URL 不起作用。要在输入值中使用引号,可以转义。

标签: php mysql quotes mariadb double-quotes


【解决方案1】:

只需使用 htmlspecialchars() 代替 str_replace()

$url = 'en.wikipedia.org%2Fwiki%2F%22Heroes%22'; // it comes in this way from the DB
//$tmp = str_replace('%22','&quot;', $url);
$url_input = htmlspecialchars(urldecode($url));
echo "<input type=\"text\" value=\"$url_input\" />";

我认为它会比这更好用

【讨论】:

  • 这似乎对避免玛莎葡萄园单引号和其他一些奇怪的东西很有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 2013-09-05
  • 2018-07-08
  • 1970-01-01
  • 2016-10-31
相关资源
最近更新 更多