<?php
/**
* Example of usage.
*
* UserAgentParser allows to parse HTTP_USER_AGENT environment value and get access to results by different ways.
*
* PHP versions 5
*
* Copyright (c) 2009 - 2012, Diptan (Ethere) Vladimir.
* @package files
* @filesource
*
*/
/**
* Output prformated data
* @param mixed $data data to output
* @return void
*/
function testParser($data) {
static $testN = 0;
echo '<pre>Test #', ++$testN, ': ', print_r($data, true), '</pre>';
}
error_reporting(E_ALL ^ E_NOTICE);
include './classes/parsers/useragentparser.php';
//$_SERVER['HTTP_USER_AGENT'] = Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7;
$parser = new UserAgentParser('Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7');
$result = $parser->parse()->toString();
testParser(unserialize($result));
/* ==============================Output
Test #1: Array
(
[0] => chrome_16.0.912.77
[browser] => chrome_16.0.912.77
[1] => 16.0.912.77
[version] => 16.0.912.77
[2] => windows_nt_5.1
[platform] => windows_nt_5.1
[3] => WEBKIT
[type] => WEBKIT
)
=============================== */
/*
* Usage of UserAgentParser::is
*/
echo '<pre>UserAgentParser::is()';
var_dump(
$parser->is('chrome'), //true
$parser->is('chrome', 16.0), //true
$parser->is('chrome', 17), //false
$parser->is('chrome', '16.0.9'), //true
$parser->is('chrome', '16.0.9', true) //false
);
echo '</pre>';
/*
* Usage of UserAgentParser::getType
*/
echo '<pre>UserAgentParser::getType()';
var_dump(
$parser->getType(), //WEBKIT
$parser->getType(UserAgentParser::WEBKIT), //true
$parser->getType(UserAgentParser::ALL & ~ UserAgentParser::WEBKIT), //false
$parser->getType(UserAgentParser::PRESTO), //false
$parser->getType(UserAgentParser::PROPRIETARY), //false
$parser->getType(UserAgentParser::UNDEFINED ^ UserAgentParser::WEBKIT), //true
$parser->getType(~ UserAgentParser::UNDEFINED), //true
$parser->getType(UserAgentParser::TRIDENT | UserAgentParser::WEBKIT) //true
);
echo '</pre>';
$parser->setUserAgent('Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.8.131 Version/11.10');
$parser->parse();
$result = (string) $parser;
testParser(unserialize($result));
/* ==============================Output
Test #2: Array
(
[0] => opera_11.10
[browser] => opera_11.10
[1] => 11.10
[version] => 11.10
[2] => windows_nt_5.1
[platform] => windows_nt_5.1
[3] => PRESTO
[type] => PRESTO
)
=============================== */
testParser($parser->parse('Opera/9.60 (J2ME/MIDP; Opera Mini/4.2.14320/554; U; cs) Presto/2.2.0')->getParsedData());
/* ==============================Output
Test #3: Array
(
[0] => opera_mini_4.2.14320
[browser] => opera_mini_4.2.14320
[1] => 4.2.14320
[version] => 4.2.14320
[2] => j2me_midp
[platform] => j2me_midp
[3] => PRESTO
[type] => PRESTO
)
=============================== */
testParser($parser->parse('Yahoo!-MMCrawler/3.x (mms dash mmcrawler dash support at yahoo dash inc dot com)')->getBrowser());
/* ==============================Output
Test #4: yahoo_mmcrawler_3
=============================== */
$parser->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.4 (Change: )');
$parser->parse();
$result = array(
$parser->getBrowser(),
$parser->getPlatform(),
$parser->getType()
);
testParser($result);
/* ==============================Output
Test #5: Array
(
[0] => arora_0.4
[1] => windows_nt_5.1
[2] => WEBKIT
)
=============================== */
testParser($parser->parse('Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; KKMAN3.2; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C)', true));
/* ==============================Output
Test #6: Array
(
[0] => kkman3.2
[browser] => kkman3.2
[1] => 3.2
[version] => 3.2
[2] => windows_nt_6.1
[platform] => windows_nt_6.1
[3] => TRIDENT
[type] => TRIDENT
)
=============================== */
$parser->parse('Mozilla/5.0 (Linux; U; Android 2.1-update1; en-au; HTC_Desire_A8183 V1.16.841.1 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17');
$result = $parser->getBrowser() . '|' . $parser->getPlatform();
testParser($result);
/* ==============================Output
Test #7: mobile_safari_4.0|android_2.1
=============================== */
$result = $parser->setUserAgent('The Bat! 4.0.14')->parse()->getBrowser();
testParser($result);
/* ==============================Output
Test #8: the_bat_4.0.14
=============================== */
$result = $parser->parse('itunes/9.0.2 (Macintosh; Intel Mac OS X 10.4.11) AppleWebKit/531.21.8')->getParsedData();
testParser($result);
/* ==============================Output
Test #9: Array
(
[0] => itunes_9.0.2
[browser] => itunes_9.0.2
[1] => 9.0.2
[version] => 9.0.2
[2] => intel_mac_os_x_10.4.11
[platform] => intel_mac_os_x_10.4.11
[3] => PROPRIETARY
[type] => PROPRIETARY
)
=============================== */
testParser($parser->parse('Mozilla/5.0 (X11; U; Linux i686; en-US; SkipStone 0.8.3) Gecko/20020615 Debian/1.0.0-3')->getParsedData());
/* ==============================Output
Test #10: Array
(
[0] => skipstone_0.8.3
[browser] => skipstone_0.8.3
[1] => 0.8.3
[version] => 0.8.3
[2] => linux_i686_debian_1.0.0
[platform] => linux_i686_debian_1.0.0
[3] => TRIDENT
[type] => TRIDENT
)
=============================== */
testParser($parser->parse('Mozilla/4.0 (compatible; MSIE 6.0; AOL 6.0; Windows NT 5.1)')->getParsedData());
/* ==============================Output
Test #11: Array
(
[0] => aol_6.0
[browser] => aol_6.0
[1] => 6.0
[version] => 6.0
[2] => windows_nt_5.1
[platform] => windows_nt_5.1
[3] => TRIDENT
[type] => TRIDENT
)
=============================== */
testParser($parser->parse('Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2a2pre) Gecko/20090901 Ubuntu/9.10 (karmic) Namoroka/3.6a2pre')->getParsedData());
/* ==============================Output
Test #12: Array
(
[0] => namoroka_3.6
[browser] => namoroka_3.6
[1] => 3.6
[version] => 3.6
[2] => linux_x86_64_ubuntu_9.10
[platform] => linux_x86_64_ubuntu_9.10
[3] => GECKO
[type] => GECKO
)
=============================== */
testParser($parser->parse('Mozilla/5.0 (X11; U; Linux amd64; rv:5.0) Gecko/20100101 Firefox/5.0 (Debian)')->getParsedData());
/* ==============================Output
Test #13: Array
(
[0] => firefox_5.0
[browser] => firefox_5.0
[1] => 5.0
[version] => 5.0
[2] => linux_amd64_debian
[platform] => linux_amd64_debian
[3] => GECKO
[type] => GECKO
)
=============================== */
|