在 PHP 中获取查询字符串不起作用
我是 php 新手,我正在尝试遵循有关如何获取查询字符串的建议。我希望以可读的格式搜索单词。
现在,字符串来自数据库,它们之前是使用 Asp.Net 中的请求对象的referer 属性收集的。但据我所知,这应该没有什么区别。
所以我尝试了这个:
function getQuery($url)
{
$processed_url = parse_url( $url );
$query_string = $processed_url['query'];
if($url != '')
{
return $query_string;
}
else
{
return $url;
}
}
所以我尝试了一个变体,它也应该提取查询字符串:
$query = parse_url($url, 6);
return $query;
嗯,这样的工作。它给了我一个查询字符串部分,但包括“q=”和所有这些,而不仅仅是查询字符串的文本。
所以我尝试了 parse_str() 函数,它应该能够解析查询字符串本身:
parse_str($query, $myArray);
return $myArray[0];
但这根本不起作用,它在结果页面(网格)中没有给我任何结果。
我做错了什么?首先使用第一种方法获取上面的查询,其次使用将查询字符串解析为其组件? (0索引只是一个例子,我想如果我设法只取出文本部分,我会稍后将其连接起来)?
I am a php newbie, and I'm trying to follow suggestions for how to get the query string. I want the words searched on in a readable format.
Now, the strings come from a database, where they have been previously collected using the request object's referer property in Asp.Net. But as far as I know that shouldn't make any difference.
So I tried this:
function getQuery($url)
{
$processed_url = parse_url( $url );
$query_string = $processed_url['query'];
if($url != '')
{
return $query_string;
}
else
{
return $url;
}
}
So I tried a variation, which should also extract the query string:
$query = parse_url($url, 6);
return $query;
Well, that sort of works. It gives me a query string part, but including the "q=" and all that, not just the text of the query string.
So I tried the parse_str() function on this, which is supposed to be able to parse the query string itself:
parse_str($query, $myArray);
return $myArray[0];
But that didn't work at all, it gives me no results in the result page (a grid).
What am I doing wrong? First of all with the first method for getting the query above, and secondly for parsing the query string into its components? (the 0 index was just an example, I thought I'd concatenate it later if I managed to get out only the text parts)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您正在尝试从字符串解析 url,而不是从当前请求解析 url(如评论中所述)
在评论中的 PHP 文档上,有一个很棒的函数,它完全可以满足您的要求,我已复制它在这里,所以它可以在将来的参考中使用 - 所有功劳都归于“Simon D” - http://dk.php.net/manual/en/function.parse-url.php#104527
这意味着它应该像这样使用:
It would seem that you are trying to parse a url from a string, not from the current request (as mentioned in comment)
On the PHP docs in the comments, there is a great function, that does exactly what you want, i have copied it here, so it can be used in future references - all credit goes to "Simon D" - http://dk.php.net/manual/en/function.parse-url.php#104527
which means it should be used like this: