Home  >  Article  >  Backend Development  >  How to verify ISBN number using PHP regular expression

How to verify ISBN number using PHP regular expression

王林
王林Original
2023-06-24 10:13:401508browse

In the book field, the ISBN number is a very important identifier. Its full name is International Standard Book Number (International Standard Book Number), which is used to uniquely identify a book. The ISBN number consists of 13 digits, including a check digit that can be used to verify the accuracy of the number. In actual development, we often need to verify ISBN numbers to ensure that they comply with specifications. PHP regular expressions provide a convenient way to verify the format of the ISBN number.

First, let us understand the structure of the ISBN number. As mentioned earlier, it consists of 13 digits, usually represented as three sets of numbers separated by hyphens. Among them, the first three groups of numbers represent the country, publisher and book title, and the hyphens between each element are used to separate them. The last digit is the check digit and is used to verify the accuracy of the entire number.

In PHP, we can use the preg_match() function to verify the ISBN number. This function matches a string using a regular expression pattern and returns the matching result. The following is a simple example for verifying a 13-digit ISBN number:

$isbn = '978-3-16-148410-0';
$pattern = '/^(d{3})-(d{1,5})-(d{1,7})-(d{1,6})-(d)$/';
if (preg_match($pattern, $isbn)) {
    echo "这是一个有效的ISBN号码。";
} else {
    echo "这不是一个有效的ISBN号码。";
}

In the above code, we first define a 13-digit ISBN number and define a 13-digit ISBN number to match this number. Regular expression pattern. The pattern consists of five groups, each matching an element in the ISBN number. Specifically, the first group matches the first three numbers, the second group matches the publisher and the numbers in the book title, the third group matches the numbers in the book title, the fourth group matches the number of publications, and the last group Matches the check digit. Note that hyphens are explicitly specified in the pattern.

Next, we use the preg_match() function to match, and determine whether the match is successful based on the return value. If successful, a prompt message is output.

To become more strict, we can also specify more detailed rules in the pattern to verify the ISBN number. For example, we could only allow numeric characters to appear in ISBN numbers. Here is a more strict example:

$isbn = '978-3-16-148410-0';
$pattern = '/^(97(8|9))(d{10})(d)$/';
if (preg_match($pattern, $isbn)) {
    echo "这是一个有效的ISBN号码。";
} else {
    echo "这不是一个有效的ISBN号码。";
}

In this example, we use a stricter pattern to match ISBN numbers. This mode only allows numeric characters to appear in the number, the first three digits must be 978 or 979, followed by 10 digits, and the last digit is the check digit. Note that we omitted the separator hyphens as they are optional. This example is more strict. If the ISBN number does not meet the requirements, the match will not be successful.

In actual development, we can adjust the strictness of the model according to actual needs. We can use optional separators or force only numeric characters to be allowed in numbers etc. Regardless of the situation, using PHP regular expressions makes it easy to verify the format of the ISBN number.

The above is the detailed content of How to verify ISBN number using PHP regular expression. 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