使用php获取主要Android版本号

I want to log the Majorn number of the Android version (first number) of a user. I'll use a PHP-script, but it don't work. The string stills empty. What do I wrong?

$txt = $_SERVER['HTTP_USER_AGENT'];
$reg = '/Android (\d+).(\d+)\s+/';
$a   = array();

preg_match($reg, $txt, $a);

$str_version = $a[1];

Thanks in advance!

Remove the whitespace escape sequence (\s) at the end of your regex, as most Android versions consists of 3 numbers separated by decimal points, for example 4.4.2:

$reg = '/Android (\d+)\.(\d+)/';

Versioning is a critical component of your app upgrade and maintenance strategy. To define the version information for your app, set values for the version settings in the Gradle build files. These values are then merged into your app's manifest file during the build process.

Two settings are available, and you should always define values for both of them:

  • versionCode — A positive integer used as an internal version number. This number is used only to determine whether one version is more recent than another, with higher numbers indicating more recent versions.

  • versionName — A string used as the version number shown to users. This setting can be specified as a raw string or as a reference to a string resource.

If you use Gradle, there is an easier way to get build version, since it is available in BuildConfig for you:

String version = BuildConfig.VERSION_NAME;