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 use PHP regex to match an exact string?
- How to use PHP regex to match whitespace?
- How to use PHP regex with zero or more occurrences?
- How to match strings starting with a certain string using PHP regex?
- How to use regex in PHP to match dot character?
- How to use PHP regex to match UTF8?
- How to use PHP regex to match special characters?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match an XML tag?
See more codes...