I need a PHP function which uses regular expressions to strip out all non-numerical characters (except ".") from a form field variable to ensure that what is left over is a valid "price" to 2 decimal places. For example:
£123.05 becomes 123.05 (no currency symbol)
$123.007 becomes 123.01 (rounded up and, again, no currency symbol)
1,000.00 becomes 1000 (no comma - trailing .00 optional)
The function should also be robust enough to handle non-sensical values such as:
and in each case return an error message.
In short, the function should be able to handle any humanly recognisable price format and return a numerical value or alert the user that a value is not valid.