php-regexHow to remove all non-numeric characters using PHP regex?
To remove all non-numeric characters using PHP regex, the following code can be used:
$string = preg_replace('/[^0-9]/', '', $string);
This code will output a string with only numeric characters, for example:
Input: $string = "Hello123World456";
## Output example
123456
The code consists of the following parts:
preg_replace()
- This is a PHP function that performs a regular expression search and replace./[^0-9]/
- This is the regular expression pattern that matches any non-numeric character.''
- This is the replacement string, which is an empty string in this case, meaning that the matched characters will be removed.$string
- This is the string that will be searched and replaced.
Helpful links
More of Php Regex
- How to match a double quote in PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to match a space using PHP regex?
- How to match a single quote in PHP regex?
- How to use named capture groups in PHP regex?
- How to match the end of a string when using regex in PHP?
- How to use an "or" condition in PHP regex?
- How to get last matched occurrence in PHP regex?
- How to use PHP regex to match a zip code?
- How to use PHP regex with zero or more occurrences?
See more codes...