Skip to content

Latest commit

 

History

History
32 lines (22 loc) · 888 Bytes

how-to-use-regex-in-php-to-match-only-digits.md

File metadata and controls

32 lines (22 loc) · 888 Bytes

How to use regex in PHP to match only digits?

// 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.

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

onelinerhub: How to use regex in PHP to match only digits?