【发布时间】: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 的值作为<a href 锚点工作顺利,然后来自表单的查询使用FILTER_SANITIZE_URL 和urlencode() 过滤以将其存储在数据库中。
有没有更好的方法来做到这一点?
【问题讨论】:
-
你可以在 HTML 中使用单引号 ->
value='$url_input'但是你可能想确保 url 也不包含单引号? -
喜欢玛莎葡萄园岛?
-
是的,就像那样。这就是为什么人们应该避免在 URL 中使用引号,维基百科只是没有收到消息,并且用双引号替换单引号也不是一种选择,因为 URL 不起作用。要在输入值中使用引号,可以转义。
标签: php mysql quotes mariadb double-quotes