php-regexHow to use regex in PHP to match any digit?
Regex (regular expressions) is a powerful tool for pattern matching in strings. In PHP, it can be used to match any digit with the \d
character class.
Example code
$string = 'This string contains 3 digits.';
if (preg_match('/\d/', $string)) {
echo 'The string contains at least one digit.';
}
Output example
The string contains at least one digit.
Code explanation
\d
: character class that matches any digitpreg_match()
: PHP function that searches a string for a pattern and returns true if found
Helpful links
More of Php Regex
- How to match a double quote in PHP regex?
- How to match a question mark in PHP regex?
- How to use named capture groups in PHP regex?
- How to get last matched occurrence in PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to match a zip code?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match UTF8?
- How to use PHP regex to match a year?
See more codes...