Extract positive and negative floating point numbers from string in php
For php, I need to extract floating point numbers from a string. I am new to regex but found a solution that works for me in most cases:
Extract floating point numbers from string in PHP
$str = '152.15 x 12.34 x 11mm';
preg_match_all('!\d+(?:\.\d+)?!', $str, $matches);
$floats = array_map('floatval', $matches[0]);
print_r($floats);
The only problem is negative values; can someone change the expression for me in such a way that negative numbers are included correctly as well?
Thank!
+3
source to share