Using is_numeric or intval is likely the best way to validate a number here, but to answer your question you could try using preg_replace instead. This example removes all non-numeric characters:
$output = preg_replace( '/[^0-9]/', '', $string );
More Related Contents:
- PHP validation/regex for URL
- PHP regular expressions: No ending delimiter ‘^’ found in
- Replace URLs in text with HTML links
- Remove non-utf8 characters from string
- How to remove accents and turn letters into “plain” ASCII characters? [duplicate]
- Regexp to add attribute in any xml tags
- PHP remove special character from string
- preg_replace: add number after backreference
- Retrieve all hashtags from a tweet in a PHP function
- How do I remove blank lines from text in PHP?
- How to use RegexIterator in PHP
- Regular expressions for a range of unicode points PHP
- How to write a recursive regex that matches nested parentheses?
- Split alphanumeric string between leading digits and trailing letters
- Allow only [a-z][A-Z][0-9] in string using PHP
- non-breaking utf-8 0xc2a0 space and preg_replace strange behaviour
- What literal characters should be escaped in a regex?
- Get last whole number in a string
- RegExp in preg_match function returning browser error
- Regex for password PHP [duplicate]
- regular expression and forward slash
- Regular Expressions: How to Express \w Without Underscore
- Youtube I.D parsing for new URL formats
- PHP and regexp to accept only Greek characters in form
- How to validate that a string only contain lowercase letters?
- Regular expression: find range except for one letter or a range
- Function To Create Regex Matching a Number Range
- How to get the shortest rather than longest possible regex match with preg_match()
- Regex to match an IP address [closed]
- PHP’s preg_replace regex that matches multiple lines