Home >Backend Development >PHP Tutorial >PHP number verification regular expression: exact match
In PHP, regular expressions can be used to easily verify numbers and ensure the accuracy and completeness of the data. This article explains how to use exact matching regular expressions to validate numbers and provides specific code examples.
First, we need to clarify the number type that needs to be verified. In practical applications, common digital verification includes integers, floating point numbers, positive integers, negative integers, etc. The verification methods for these situations are introduced below.
Integer verification:
To verify whether a string is an integer, you can use the following regular expression:
/^-?d $/
Among them, ^ represents the starting position of the matching string, $ represents the end position of the matching string, -? represents the optional negative sign, and d represents one or more numbers. With this regular expression, you can ensure that the input string contains only integers.
PHP code example:
$number = "12345"; if (preg_match('/^-?d $/', $number)) { echo "Digital verification passed"; } else { echo "The input is not an integer"; }
If you need to verify floating point numbers, that is, numbers containing decimal points, you can use the following regular expression:
If you need to verify a positive integer, you can use the following regular expression:
To validate a negative integer, you can use the following regular expression:
The above is the detailed content of PHP number verification regular expression: exact match. For more information, please follow other related articles on the PHP Chinese website!