RegEx ile farklı bir kod vereyim. Google ve Bing ile uyumludur.

preg_match("/[\&\?]q=([^&]*)/", $_SERVER['HTTP_REFERER'], $matches);
if($matches[1])
{
$query = rawurldecode(str_replace('+', ' ', $matches[1]));
}