Function to Remove Dollar Signs and Commas From a Number

I needed a function to turn a string value dollar with commas into a decimal I could insert into MySQL. This function below will take a string like “$1,423,021.99” and return a float of 1423021.99 – removing any dollar signs, commas, anything other than decimals and digits. It’s sort of the opposite of the php number_format function.

Method 1 – regular expression

function tofloat($numberString) {
	return floatval(preg_replace("/[^0-9.]/", '', $numberString));


Method 2 – string replacement

$cleanNumber = (double)str_replace(array(',','$'), '', $numberString);