PHP Regular Expression Exercises: Remove nonnumeric characters except comma and dot
Write a PHP script to remove nonnumeric characters except comma and dot.
Sample string : '$123,34.00A'
Visual Presentation:

Sample Solution:
PHP Code:
Output:
12,334.00
Explanation:
In the exercise above,
The given PHP code removes all characters except digits (0-9), comma (,), and period (.) from the string '$str1'.
It uses the 'preg_replace' function with a regular expression pattern '"/[^0-9,.]/"' to match any character that is not a digit, comma, or period, and replaces those characters with an empty string.
Finally, it echoes the modified string.
Flowchart :

PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP script that removes the whitespaces from a string.
Next: Write a PHP script to remove new lines (characters) from a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.