Google如何处理两个具有不同值的相同URL参数? [英] How does google handle two same URL parameters with different values?
问题描述
只要看看这个URL,您就会知道我的意思.
Just take a look at this url, and you will know what I mean.
https://www.google.com/search?q=site%3Aphpjs.org&q=date
当我转到该URL时,谷歌搜索栏中的搜索词是site:phpjs.org date
.
And when I go to that url, the search term in google's search bar is site:phpjs.org date
.
Google如何将两个参数一起变形",如何在PHP中做到这一点?
How does Google 'morph' the two parameters together, and how would one do it in PHP?
推荐答案
Google使用相同的q
变量来完成相同的操作,而不是对空间进行编码.
Instead of encoding the space, Google uses the same q
variable to accomplish the same thing.
不幸的是,PHP没有内置的功能来执行此操作,因为除非使用[]
后缀,否则连续出现的相同查询字符串参数将覆盖第一个参数.
Unfortunately, PHP doesn't have the built-in ability to do this, because successive occurrences of the same query string parameter will overwrite the first one, unless the []
suffix is used.
您将需要以下内容:
$params = array();
foreach (explode('&', $_SERVER['QUERY_STRING']) as $param) {
list($name, $value) = explode('=', $param, 2);
$params[] = array(urldecode($name) => urldecode($value));
}
$params
的内容:
array(
array('q' => 'site:phpjs.org'),
array('q' => 'date'),
);
或者,您可以将循环体更改为此:
Alternatively, you can change the loop body to this:
$params[urldecode($name)][] = urldecode($value);
这会将$params
更改为:
array('q' => array('site:phpjs.org', 'date'));
这样做会更容易:
join(' ', $params['q']);
// "site:phpjs.org date"
这篇关于Google如何处理两个具有不同值的相同URL参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!