I want to get the version number for some string in php. Here is the sample code
<?php
function getVersion($str) {
preg_match("/.*((?:[0-9]+\.?)+)/i", $str, $matches);
return $matches[1];
}
print_r(getVersion("ansitl1-isam-6.0 1.0 9.7.03 418614 +"));
print_r(getVersion("ams-ef 9.6.06ef4 - 394867"));
?>
for input string ansitl1-isam-6.0 1.0 9.7.03 418614 +
output should be 9.7.03 for input string ams-ef 9.6.06ef4 - 394867
output should be 9.6.06
How to achieve this?
If the pattern is always num.num.num preceded by a space.
(?<= )\d+\.\d+\.\d+
See this demo at Regex101 or a PHP demo at tio.run
There is not much Regex magic used here, just a lookbehind to check, there is a space before.
Instead it can also be done by a caturing group and getting $out[1]
like in this demo .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.