// plain
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.
$string = '123abc456';
if (preg_match('/\d/', $string)) {
echo 'The string contains digits.';
}
The string contains digits.
\d
: This is the character class for digits.preg_match()
: This is the PHP function used to match a regex pattern against a string.
onelinerhub: How to use regex in PHP to match only digits?