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 use PHP regex to match a nbsp HTML whitespace?
- How to match a space using PHP regex?
- How to use PHP regex to match an exact string?
- How to match a single quote in PHP regex?
- How to use PHP regex to match whitespace?
- How to match strings starting with a certain string using PHP regex?
- How to remove non-printable characters using PHP regex?
- How to match a double quote in PHP regex?
- How to use PHP regex with the "x" modifier?
See more codes...