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