PHP Explode,向 URL 添加变量
我已经在用户输入变量上使用爆炸来获取搜索词数组。 这些搜索词需要按以下样式添加到 URL 末尾 = www.example.com/$search[0]%20$search[1]
但是,我不知道会有多少个搜索词,例如可能有 5 个,也可能有 1 个,如果我添加 5 个搜索[] 变量到末尾并且用户输入 1 个搜索词,我得到 4 个缺少变量的错误。
我有什么想法可以解决这个问题吗?
谢谢。
Ive used explode on a user input variable to get an array of search terms.
These search terms need to be added to the end of a URL in the following style =
www.example.com/$search[0]%20$search[1]
However, I dont know how many search terms theres going to be, eg there could be 5, or there could be 1, and if I add 5 search[] variables to the end and the user inputs 1 search term, I get 4 errors for the missing variables.
Any ideas how I can solve this problem?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
explode
的反面是implode
。用那个。The opposite of
explode
isimplode
. Use that.这就是全部内容了。
That's really all there is to it.
使用 implode 函数,并以
%20
作为分隔符,以及您的 params 数组作为数据。这将返回一个字符串,其中包含所有数组值,它们之间用
%20
连接起来use the implode function with
%20
as the delimiter, and your params array as the data.this will return a string that consists of all the array values concatenated with
%20
in between themPHP 已经附带了一个方便的函数来构建查询字符串。查找
http_build_query
PHP already comes with a handy function to build query strings.. look up
http_build_query
使用
isset()
检查变量是否存在。它也适用于数组。请参阅手册: http://www.php.net/manual/en/function .isset.phpUse
isset()
to check if the variable exists. It also works on arrays. See the manual: http://www.php.net/manual/en/function.isset.php