PHP and Money, converting money to cents

currencymoney-formatMySQLPHP

So I have done a fair bit of research on how to store "money" in a database and I think the system I want to use is

Converting Money into CENTS and then storing the CENTS in a MySQL DB with a field type of DECIMAL (19,4).

My question is, IF I have an input field from the user… how do I deal with multiple input types.
IE:

$input = "1,346.54"
$input = "10,985.23"
$input = "110,400.50"
$input = "1033.44"

etc etc…

What would be the best method for converting this to CENTS? As we have to deal with 'strings' and convert them to INT, and divide by 100… Everything that I try throws issues because of the "comma" separation with the numbers.

Any thoughts or direction would be greatly appreciated.

Best Answer

function getMoneyAsCents($value)
{
    // strip out commas
    $value = preg_replace("/\,/i","",$value);
    // strip out all but numbers, dash, and dot
    $value = preg_replace("/([^0-9\.\-])/i","",$value);
    // make sure we are dealing with a proper number now, no +.4393 or 3...304 or 76.5895,94
    if (!is_numeric($value))
    {
        return 0.00;
    }
    // convert to a float explicitly
    $value = (float)$value;
    return round($value,2)*100;
}