PHP 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

 

Method 2 – string replacement