<?php
$version = '5.5.58-0ubuntu0.14.04.1';
echo '<pre>';
var_dump(preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts));
if (!preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) {
echo "RegEx failed on " . $version . PHP_EOL;
} else {
echo "RegEx extracted version parts" . PHP_EOL;
var_dump($versionParts);
}
function errorValue($error)
{
return defined($error) ? constant($error) : $error;
}
$errors = array(
'PREG_NO_ERROR',
'PREG_INTERNAL_ERROR',
'PREG_BACKTRACK_LIMIT_ERROR',
'PREG_RECURSION_LIMIT_ERROR',
'PREG_BAD_UTF8_ERROR',
'PREG_BAD_UTF8_OFFSET_ERROR',
'PREG_JIT_STACKLIMIT_ERROR'
);
$pregErrors = array_combine(
array_map('errorValue', $errors),
$errors
);
echo $pregErrors[preg_last_error()] . PHP_EOL;
echo '</pre>';
- Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
- <pre>int(1)
RegEx extracted version parts
array(7) {
[0]=>
string(6) "5.5.58"
["major"]=>
string(1) "5"
[1]=>
string(1) "5"
["minor"]=>
string(1) "5"
[2]=>
string(1) "5"
["patch"]=>
string(2) "58"
[3]=>
string(2) "58"
}
PREG_NO_ERROR
</pre>
preferences:
48.71 ms | 408 KiB | 5 Q