使用PHP从(搜索引擎)引荐网址获取关键字

pnm*_*123 7 php url keyword preg-match

我想从引荐来源网址获取搜索关键字.目前,我正在使用以下代码用于Google网址.但有时它不起作用......

$query_get = "(q|p)";
$referrer = "http://www.google.com/search?hl=en&q=learn+php+2&client=firefox";
preg_match('/[?&]'.$query_get.'=(.*?)[&]/',$referrer,$search_keyword);
Run Code Online (Sandbox Code Playgroud)

有没有其他/清洁/工作方式来做到这一点?

谢谢你,普拉萨德

Wil*_*iam 14

如果您使用的是PHP5,请查看http://php.net/parse_urlhttp://php.net/parse_str

例:


// The referrer
$referrer = 'http://www.google.com/search?hl=en&q=learn+php+2&client=firefox';

// Parse the URL into an array
$parsed = parse_url( $referrer, PHP_URL_QUERY );

// Parse the query string into an array
parse_str( $parsed, $query );

// Output the result
echo $query['q'];