Home  >  Article  >  Backend Development  >  How to validate book ISBN number format using regular expression in PHP

How to validate book ISBN number format using regular expression in PHP

王林
王林Original
2023-06-22 09:07:341567browse

Regular expression is a powerful text matching tool that can effectively verify whether the input data meets the specified format requirements. In applications such as library management systems, ISBN (International Standard Book Number) is a data format worthy of attention, and its format has certain rules. In this article, we will introduce how to use regular expressions in PHP to validate the book ISBN number format to ensure that the system can correctly handle the ISBN number related business logic.

  1. ISBN number format rules

ISBN number is a standard number used to uniquely identify a book, usually including ISBN-10 and ISBN-13 formats. The ISBN-10 format consists of 10 digits, where the last digit may be an X, while the ISBN-13 format consists of 13 digits. In this article, we will focus on the ISBN-13 format and verify that it is well-formed using regular expressions.

The format rules of ISBN-13 numbers are as follows:

(1) The first three digits indicate the country or language difference of the book. There will be different prefix digits for different countries or regions. For example, the prefix numbers for China are 978 or 979.

(2) The next 9 digits are the book’s registration number, assigned to each book by the publisher.

(3) The last digit is the check digit (Check Digit). The calculation method of the check digit is as follows:

1) Weighted sum of the first 12 digits according to the weight, Among them, the weight of even-numbered digits is 3, and the weight of odd-numbered digits is 1;

2) Divide the result by 10 to get the remainder, subtract the remainder from 10, and the difference is the check code .

For example, the ISBN-13 number of the book "PHP and MySQL Development from Beginner to Master" is 978-7-81037-609-2, where the weighted sum of the first 12 digits is 1×9 3×7 1×8 3×8 1×1 3×0 1×3 3×7 1×6 3×0 1×9 3×2=99, take the remainder as 9, subtract the remainder from 10 to get 1, which is the last The bit check code is 1.

  1. Use regular expressions in PHP to verify the ISBN number format

In order to verify whether the ISBN number format is correct, we can use the regular expression function preg_match() in PHP , the syntax is as follows:

preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false

Among them, $pattern is the regular expression pattern, $subject is the string to be matched, $matches is the matching result array, $flags is the option flag, and $offset is the starting match Location. When the match is successful, the function returns 1; when the match fails, the function returns 0.

The following is a sample code that uses PHP regular expressions to verify the ISBN-13 number format:

function is_valid_isbn($isbn) {
    $pattern = '/^97[89]d{10}$/'; // 匹配 ISBN-13 号码格式的正则表达式
    if(preg_match($pattern, $isbn)) { // 使用 preg_match() 函数进行匹配
        $digits = str_split($isbn);
        $check_digit = 0; // 校验码的计算过程
        for($i=0; $i<12; $i+=2) {
            $check_digit += $digits[$i] + 3 * $digits[$i+1];
        }
        $check_digit = (10 - $check_digit % 10) % 10;
        return $digits[12] == $check_digit;
    }
    else {
        return false;
    }
}

// 验证 ISBN-13 号码格式
$isbn1 = '978-7-81037-609-1';
$isbn2 = '978-7-81037-609-2';
if(is_valid_isbn(str_replace('-', '', $isbn1))) {
    echo $isbn1.' 是一个有效的 ISBN-13 号码。';
}
else {
    echo $isbn1.' 不是一个有效的 ISBN-13 号码。';
}
if(is_valid_isbn(str_replace('-', '', $isbn2))) {
    echo $isbn2.' 是一个有效的 ISBN-13 号码。';
}
else {
    echo $isbn2.' 不是一个有效的 ISBN-13 号码。';
}

In the above sample code, we first define a function named is_valid_isbn(), Its parameter is the ISBN number to be verified. This function first uses a regular expression to verify whether the format of the ISBN-13 number is correct. If the format is correct, the check code is calculated and compared with the last digit to determine whether the ISBN number is valid. Among them, str_replace('-', '', $isbn) is used to remove the dash separator in the ISBN number for calculation.

Finally, we can test it on some ISBN numbers to verify the effectiveness of the function. The function returns true when the entered ISBN number is in the correct format and the checksum is calculated correctly; otherwise, it returns false. In this way, we can effectively verify whether the entered ISBN number complies with the rules and ensure that the system can correctly handle ISBN-related business logic.

  1. Conclusion

This article introduces how to use regular expressions in PHP to verify whether the format of the book ISBN-13 number is correct, and can effectively determine whether the input data conforms to rule. In actual development, we can define different regular expression patterns according to actual needs to adapt to different data format verification needs. Regular expressions are a very powerful text matching tool, and it is crucial for developers to master the skills of using regular expressions.

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