问题
I'm using the get_browser()
function, and I installed the latest php_browscap.ini
file from http://tempdownloads.browserscap.com/, updated my php.ini file to reflect the new location.
Here's my simple code I've gleaned from PHP's site:
<?php
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo "<br>";
$browser = get_browser(null, true);
print_r($browser);
?>
And the result is this:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
Array ( [browser_name_regex] => �^mozilla/5\.0 \(.*windows nt 6\.1.*wow64.*\).*gecko/.*firefox/21\..*$� [browser_name_pattern] => Mozilla/5.0 (*Windows NT 6.1*WOW64*)*Gecko/*Firefox/21.* [parent] => Firefox 21.0 [platform] => Win7 [platform_version] => 6.1 [win32] => [win64] => 1 [comment] => Firefox 21.0 [browser] => Firefox [version] => 21.0 [majorver] => 21 [minorver] => 0 [beta] => 1 [frames] => 1 [iframes] => 1 [tables] => 1 [cookies] => 1 [javascript] => 1 [javaapplets] => 1 [cssversion] => 3 [alpha] => [win16] => [backgroundsounds] => [vbscript] => [activexcontrols] => [ismobiledevice] => [issyndicationreader] => [crawler] => [aolversion] => 0 )
I'm seeing a weird character in the [browser_name_regex] section.
Is that expected?
Also, I
回答1:
Although it's not expected, but it's allowed. My output from the get_browser()
function was [browser_name_regex] => §^.*$§
. It just has two paragraphs instead of exerted slashes /^.*$/
.
Explanation:
Delimiters can be any non-alphanumeric, non-whitespace ASCII character except the backslash (\) and the null byte. (link)
What you see is merely a pcre pattern wrapped with delimiter which is \xA7. To get browsers matching you'll need a browscap.ini which is downloadable by the links on the doc page. (link)
回答2:
@Stano, thanks for the explanation. Based on your answer and the information found at https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=612364 which explains the bug more throughly, i've created some lines of code which solves the problem.
The same question has been asked at Getting illegal characters in get_browser() method run in php
I've posted my answer both here and there.
Code:
if (function_exists('get_browser') && ini_get('browscap')) {
$browser_info = get_browser(null, true);
if (function_exists('mb_convert_encoding')) $browser_info['browser_name_regex'] = mb_convert_encoding($browser_info['browser_name_regex'], "UTF-8", "ISO-8859-1");
print_r($browser_info);
}
来源:https://stackoverflow.com/questions/17392256/php-get-browser-results