Use this snippet to extract any float out of a string. You can choose how a single dot is treated with the (bool) 'single_dot_as_decimal' directive.
This function should be able to cover almost all floats that appear in an european environment.
<?php
function float($str, $set=FALSE)
{
if(preg_match("/([0-9\.,-]+)/", $str, $match))
{
$str = $match[0];
if(strstr($str, ','))
{
$str = str_replace('.', '', $str); $str = str_replace(',', '.', $str); return floatval($str);
}
else
{
if(preg_match("/^[0-9]*[\.]{1}[0-9-]+$/", $str) == TRUE && $set['single_dot_as_decimal'] == TRUE)
{
return floatval($str);
}
else
{
$str = str_replace('.', '', $str); return floatval($str);
}
}
}
else
{
return 0;
}
}
echo float('foo 123,00 bar'); echo float('foo 123.00 bar' array('single_dot_as_decimal'=> TRUE)); echo float('foo 123.00 bar' array('single_dot_as_decimal'=> FALSE)); echo float('foo 222.123.00 bar' array('single_dot_as_decimal'=> TRUE)); echo float('foo 222.123.00 bar' array('single_dot_as_decimal'=> FALSE)); echo float('foo 123,-- bar'); ?>
Big Up.
Philipp