Let's say I have a form where I ask for a product's price. Basically, I don't consider the case where the user adds the thousands comma, that is: 1,000.00 or 1,000,000.00
I just consider the case where the user inputs 1000.00 or 1000,00 or 1000000.00 but never 1,000.00, or 10,000,000.00 or, even worst, 10.000.000,00
I came up with this function.
function getPrice($price)
{
if (is_string($price)) {
if (strpos($price, ",") !== false) {
$price = str_replace(',', '.', $price);
}
}
return (is_numeric($price)) ? floatval($price) : false;
}
Do you consider safe this function? Can you point improovements?
,or.as the decimal separator.