Skip to content

Instantly share code, notes, and snippets.

@xoxefdp
Last active November 1, 2019 22:04
Show Gist options
  • Save xoxefdp/e74ef30ca4721f2588367e2de8ebe553 to your computer and use it in GitHub Desktop.
Save xoxefdp/e74ef30ca4721f2588367e2de8ebe553 to your computer and use it in GitHub Desktop.
Fixes wrong formatted browsercap.ini file from https://browscap.org (PHP Versions)
<?php
// https://browscap.org/stream?q=Full_PHP_BrowsCapINI
$full_php_browscap = file('full_php_browscap.ini');
// Let\'s Encrypt flag
$count = 0;
foreach( $full_php_browscap as &$row ) {
if ( $row[ 0 ] == '[' ) {
$row = str_replace( ';', '\\;', $row );
}
// Let\'s Encrypt single quote
if ( ( strpos($row, "'") !== false ) && ( $count < 3 ) ) {
$row = str_replace( "'", "\\'", $row );
$count++;
}
}
file_put_contents( 'browscap.ini', $full_php_browscap );
echo 'Fixing full_php_browscap.ini to browscap.ini DONE';
?>
<?php
// https://browscap.org/stream?q=Lite_PHP_BrowsCapINI
$lite_php_browscap = file('lite_php_browscap.ini');
// Let\'s Encrypt flag
$count = 0;
foreach( $lite_php_browscap as &$row ) {
if ( $row[ 0 ] == '[' ) {
$row = str_replace( ';', '\\;', $row );
}
// Let\'s Encrypt single quote
if ( ( strpos($row, "'") !== false ) && ( $count < 3 ) ) {
$row = str_replace( "'", "\\'", $row );
$count++;
}
}
file_put_contents( 'browscap.ini', $lite_php_browscap );
echo 'Fixing lite_php_browscap.ini to browscap.ini DONE';
?>
<?php
// https://browscap.org/stream?q=PHP_BrowsCapINI
$php_browscap = file('php_browscap.ini');
// Let\'s Encrypt flag
$count = 0;
foreach( $php_browscap as &$row ) {
if ( $row[ 0 ] == '[' ) {
$row = str_replace( ';', '\\;', $row );
}
// Let\'s Encrypt single quote
if ( ( strpos($row, "'") !== false ) && ( $count < 3 ) ) {
$row = str_replace( "'", "\\'", $row );
$count++;
}
}
file_put_contents( 'browscap.ini', $php_browscap );
echo 'Fixing php_browscap.ini to browscap.ini DONE';
?>
@xoxefdp
Copy link
Author

xoxefdp commented Nov 1, 2019

Using get_browser()

PHP actually has a built-in method of parsing the browscap.ini file. However, for this method you must manually download the correct browscap.ini (any of the PHP versions) and configure PHP for the correct directory.

To use get_browser() on PHP a browscap.ini is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment