Home  >  Article  >  Backend Development  >  PHP number verification regular expression: exact match

PHP number verification regular expression: exact match

WBOY
WBOYOriginal
2024-03-20 13:27:03433browse

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.

  1. 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";
}
  1. ##Floating point number verification:

    If you need to verify floating point numbers, that is, numbers containing decimal points, you can use the following regular expression:

    /^-?d (.d )?$/
    Where, .d represents the decimal part, ? represents that the decimal part is optional. Through this regular expression, you can verify whether it is a floating point number. 

PHP code example:

$number = "3.14"; if (preg_match('/^-?d (.d )?$/', $number)) { echo "Floating point number verification passed"; } else { echo "Input is not a floating point number"; }
  1. Positive integer verification:

    If you need to verify a positive 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, and d represents one or more numbers. This regular expression ensures that the input string is a positive integer. 

PHP code example:

$number = "123"; if (preg_match('/^d $/', $number)) { echo "Positive integer verification passed"; } else { echo "The input is not a positive integer"; }
  1. Negative integer validation:

    To validate a negative integer, you can use the following regular expression:

    /^-?d $/
    Same as integer validation regular expression. Just simply determine whether it contains a negative sign. 

PHP code example:

$number = "-123"; if (preg_match('/^-?d $/', $number)) { echo "Negative integer verification passed"; } else { echo "The input is not a negative integer"; }
Through the above regular expressions and code examples, we can easily implement accurate verification of numbers in PHP to ensure the accuracy and completeness of the data. In practical applications, the appropriate verification method is selected according to specific needs to ensure the validity of the data. 

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn