Skip to content
Advertisement

Remove non-numeric characters (except periods and commas) from a string

If I have the following values:

JavaScript

How can I create a new variable and set it to a copy of the data that has any non-numeric characters removed, with the exception of commas and periods? The values above would return the following results:

JavaScript

Advertisement

Answer

You could use preg_replace to swap out all non-numeric characters and the comma and period/full stop as follows:

JavaScript

The pattern can also be expressed as /[^d,.]+/

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement