如何在数组中逐字检索 $_GET 值
我有一个输入字段,提交时会在网址中附加这样的代码:
?searchval=this+is+a+test
在我的 php 文件中,我现在想逐字存储该字符串一个数组,在空格所在的地方分割它......像这样:
array[0] = "this";
array[1] = "is";
array[2] = "a";
array[3] = "test";
完成这个任务最有效的方法是什么?我已经搞乱了 substr() 和 preg_replace() 但整个事情非常混乱,我认为必须有一种更简单的方法,考虑到 $_GET 也在 URL 中用 + 分割单词。
任何帮助将不胜感激。 :)
I have an input field, that when submitted attaches a code like this to the url:
?searchval=this+is+a+test
In my php file I now want to store this string word by word in an array, splitting it where the spaces are... Something like this:
array[0] = "this";
array[1] = "is";
array[2] = "a";
array[3] = "test";
What would be the most effective way to accomplish this? I have messed around with substr() and preg_replace() but the whole thing is very messy and I figured that there must be an easier way, considering the $_GET also splits the words with +'s in the URL.
Any help would be greatly appreciated. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找
explode
。我应该在这里指出,虽然您可能会在 URL 中看到加号,但当填充
$_GET
时,PHP 会自动将这些加号转换为空格。用技术术语来说,查询字符串变量的名称和值是 URL 解码的(这正是您也可以使用urldecode
)。You are looking for
explode
.I should note here that while you may see plus signs in the URL, these are automatically translated to spaces by PHP when
$_GET
is populated. In technical terms, the names and values of the query string variables are URL-decoded (exactly what you can also do in code withurldecode
).