I have a string like:
$string = "P 1 BNN-01 HV A7weg letsel (Auto) A7 Re 138,3 HASKERHORNE.";
I want to get the "138,3" part to use as variable like $number = "138,3". I think I can use preg_match_all and then use the matches array, but I cant find the correct regex for it.
So the string can contain multiple numbers, but I need only the one with a comma in it. It can contain a maximum of 3 numbers before the comma, and only one after.
$pattern = "^-?\d+(,\d+)*(\.\d+(e\d+)?)?$";
if(preg_match_all($pattern, $string, $matches, PREG_PATTERN_ORDER)) {
print_r($matches);
}
$pattern = '~\d+,\d+~';and usepreg_matchsince you need a single match.$number = preg_match('~\d+,\d+~', $string, $matches) ? $matches[0] : null;