I've been using this...
echo PHP_VERSION;
...to display the version of PHP running on the server, which usually displays something like...
5.4.6.1
But I've just discovered that on some servers I get more than the version number, and instead this gets displayed:
5.4.6.1ububtu1.8
Is there a way I can get just the numbers?
The reason being is I need to check the version of PHP and display a message, something like...
$phpversion = PHP_VERSION;
if($phpversion >= "5.5") {
echo "All good";
} else {
echo "Your version of PHP is too old";
}
...but of course if $phpversion contains anything other than numbers then this script won't work properly.
Thanks in advance.
To get full version (5.4.6.1)
<?php
preg_match("#^\d+(\.\d+)*#", PHP_VERSION, $match);
echo $match[0];
It will return 5.4.6.1 in your example
To get version as you need (5.4)
preg_match("#^\d.\d#", "5.4.6.1ububtu1.8", $match);
echo $match[0];
It will return 5.4 in your example
Test here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With