PHP - Split a sequence of numbers into decimal

I have a number that represents the software version (for example: 1.2.0.14), and I need to separate each number divided by the decimal, and store each number as a separate variable.

Example:

Original number - 1.2.0.14

$current_version_major = 1;
$current_version_minor = 2;
$current_version_revision = 0;
$current_version_build = 14;

What would be the most effective way to do this?

+3
source share
5 answers

If you really don't need to store the version fields separately and just want to compare the two versions, then version_comparesometimes a good alternative is:

switch (version_compare("1.2.0.14", "1.2.0.22")) {
    case -1:  // second version number is higher
    case  0:  // both identical
    case +1:  // second version is older
}

( rc beta), version_compare , .14 , .2. 1.0-2

version_compare("2.0", "1.0", ">") .

+1

, .

, , explode list :

list(
      $current_version_major,
      $current_version_minor,
      $current_version_revision,
      $current_version_build) = explode('.', $version_number);

:

+6
list($current_version_major,$current_version_minor,$current_version_revision,$current_version_build) = explode('.',$version);
+2

:

$version = explode(".", "1.2.0.14");

$version[0] "1" . $version[1] "2" , $version[2] "0" , $version[3] "14" .

+1

PHP has an explode function that returns you elements as an array, where the separator is "." (your "decimal").

+1
source

Source: https://habr.com/ru/post/1788660/


All Articles