php-regexHow to use regex in PHP to match only digits?
Regex (regular expressions) is a powerful tool for matching patterns in strings. In PHP, you can use regex to match only digits with the \d character class.
Example code
$string = '123abc456';
if (preg_match('/\d/', $string)) {
echo 'The string contains digits.';
}
Output example
The string contains digits.
Code explanation
\d: This is the character class for digits.preg_match(): This is the PHP function used to match a regex pattern against a string.
Helpful links
More of Php Regex
- How to use PHP regex to match UUID?
- How to use PHP regex to match a word?
- How to use PHP regex to match special characters?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match URL path?
- How to use PHP regex to match time?
- How to remove non-printable characters using PHP regex?
- How to use PHP regex in a case insensitive mode?
- How to use the "s" modifier in PHP regex?
- How to use PHP regex to match UTF8?
See more codes...