从GA cookie中提取关键字的问题

时间:2012-02-13 20:59:47

标签: php javascript

我目前在从cookie中提取关键字时遇到了一些麻烦,我只想要关键字,这些都是我的代码:

function parse_ga_cookie($cookie) 
{ 

 $values = sscanf($cookie, '%d.%d.%d.%d.utmcsr=%[^|]|utmccn=%[^|]|utmcmd=%[^|]|utmctr=%[^|]'); 

    if (count($values) !== 8) { 
        // return false; trigger_error(...); ... or whatever you like 
        throw new InvalidArgumentException("Cookie value '$cookie' does not conform to the __utmz pattern"); 

    } 
    $keys = array('domain', 'timestamp', 'visits', 'sources', 'campaign', 'source', 'medium', 'keyword'); 

    return array_combine($keys, $values); 

} 

print_r(parse_ga_cookie($_COOKIE['__utmz']));

这将提供所有键,但如果我只打印关键字,它只会给我array():/

1 个答案:

答案 0 :(得分:0)

function parse_ga_cookie($cookie) 
{ 

 $values = sscanf($cookie, '%d.%d.%d.%d.utmcsr=%[^|]|utmccn=%[^|]|utmcmd=%[^|]|utmctr=%[^|]'); 

    if (count($values) !== 8) { 
        // return false; trigger_error(...); ... or whatever you like 
        throw new InvalidArgumentException("Cookie value '$cookie' does not conform to the __utmz pattern"); 

    } 
    $keys = array('domain', 'timestamp', 'visits', 'sources', 'campaign', 'source', 'medium', 'keyword'); 

    return array_combine($keys, $values); 

} 

$keyword=parse_ga_cookie($_COOKIE['__utmz']);
$keyword=$keyword['keyword'];