分隔 URL 中的值,而不是使用 &
URL 中的每个参数可以有多个值。我怎样才能将它们分开?这是一个例子:
http://www.example.com/search?queries=cars,phones
所以我想搜索两种不同的东西:汽车和电话(这只是一个人为的例子)。问题在于分隔符,即逗号。用户可以在搜索表单中输入逗号作为查询的一部分,然后这就会搞砸。我可以有 2 个单独的 URL 参数:
http://www.example.com/login?name1=harry&name2=bob
那里没有真正的问题,事实上,我认为这就是 URL 的设计方式来处理这种情况。但我不能在我的特殊情况下使用它。需要一个单独的长篇文章来说明原因......我需要简单地分离这些值。
我的问题基本上是,是否存在无法在可用作分隔符的表单(文本区域或输入)中输入的 URL 可编码字符或值?就像空字符一样?还是看不见的角色?
更新:感谢大家的快速回复。我也应该列出相同的参数名称示例,但在我的情况下顺序很重要,因此这也不是一个选项。我们通过使用 %00 URL 编码字符 (UTF-8 \u0000) 作为值分隔符解决了这个问题。
Each parameter in a URL can have multiple values. How can I separate them? Here's an example:
http://www.example.com/search?queries=cars,phones
So I want to search for 2 different things: cars and phones (this is just a contrived example). The problem is the separator, a comma. A user could enter a comma in the search form as part of their query and then this would get screwed up. I could have 2 separate URL parameters:
http://www.example.com/login?name1=harry&name2=bob
There's no real problem there, in fact I think this is how URLs were designed to handle this situation. But I can't use it in my particular situation. Requires a separate long post to say why... I need to simply separate the values.
My question is basically, is there a URL encodable character or value that can't possibly be entered in a form (textarea or input) which I can use as a separator? Like a null character? Or a non-visible character?
UPDATE: thank you all for your very quick responses. I should've listed the same parameter name example too, but order matters in my case so that wasn't an option either. We solved this by using a %00 URL encoded character (UTF-8 \u0000) as a value separator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
标准方法是使用相同的键名两次。
大多数表单库将允许您自动将其作为数组访问。 (如果您使用 PHP(并且使用
$_POST/GET
并且不重新发明轮子),您需要将名称更改为queries[]
。)The standard approach to this is to use the same key name twice.
Most form libraries will allow you to access it as an array automatically. (If you are using PHP (and making use of
$_POST/GET
and not reinventing the wheel) you will need to change the name toqueries[]
.)您可以为它们指定相同的参数名称。
一般的服务器端 HTTP API 能够以数组的形式获取它们。根据您的问题历史记录,您正在使用 JSP/Servlet,因此您可以使用
HttpServletRequest#getParameterValues()
为此。You can give them each the same parameter name.
The average server side HTTP API is able to obtain them as an array. As per your question history, you're using JSP/Servlet, so you can use
HttpServletRequest#getParameterValues()
for this.只需对用户输入进行 URL 编码,使其逗号变为 %2C。
Just URL-encode the user input so that their commas become %2C.
提出您自己的分隔符,该分隔符不太可能在查询中输入。例如两个下划线
'__'
。Come up with your own separator that is unlikely to get entered in a query. Two underscores
'__'
for example.为什么不直接做“||”之类的事情呢?任何在搜索区域中输入该内容的人都可能在键盘上睡着了:}然后在后端将其爆炸。
Why not just do something like "||"? Anyone who types that into a search area probably fell asleep on their keyboard :} Then just explode it on the backend.
最简单的方法是使用自定义分隔符,例如 [!!ValSep!!]。
easiest thing to do would be to use a custom separator like [!!ValSep!!].