0

I have an array $cfg=Array('5.3', '5.4') which contains major Php versions. I want to determine major from version. For example if input is 5.4.10-1ubuntu3 the response should be 5.4. My code is this:

 foreach ($cfg as $key => $phpMajor) {
    if (version_compare($phpVersion, $phpMajor, '>=') and version_compare($phpVersion, $cfg[$key+1], '<')) {
        var_dump($phpMajor);
    }

}

But this is not cover my all possibilities. Can you help me please?

2
  • php.net/phpversion Example #2 PHP_VERSION_ID example and usage Commented Feb 6, 2015 at 7:26
  • 2
    Where do you get that version string from? Maybe you're just looking for the PHP_VERSION, PHP_MAJOR_VERSION, PHP_MINOR_VERSION constants? Commented Feb 6, 2015 at 7:27

1 Answer 1

1
<?php
$subject = '5.4.10-1ubuntu3'; 
$matches = array();
$pattern = '#[0-9]{1,1}.[0-9]{1,2}#';
if (preg_match ($pattern , $subject, $matches) == 1) {
    print "Major version : " . $matches[0] ;
}else {
    print "Can not found version";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.