Will Bonde
Will Bonde

Reputation: 538

Not Operator for PCRE using php preg_replace

Is there a way to do a not operator for a PCRE in the PHP preg_replace function? I'm trying to filter a currency string (ex: $1,000.00) down to the float value (1000.00). My thought is to preg_replace on anything that is NOT 0-9 or the decimal point. So, essentially, I want to invert the pattern /([0-9.])/ - is there a way to do that?

Upvotes: 0

Views: 3053

Answers (1)

Esailija
Esailija

Reputation: 140234

preg_replace( '/[^0-9.]+/', '', '$1,000.00' )

results in 1000.00.

the caret at the beginning marks that all the characters that aren't in the set (0-9 and a dot) will match and thus will be replaced with nothing.

Upvotes: 3

Related Questions