Home >Backend Development >PHP Tutorial >PHP regular expression to verify that the input string is in the correct ISBN encoding format

PHP regular expression to verify that the input string is in the correct ISBN encoding format

WBOY
WBOYOriginal
2023-06-24 21:18:321442browse

ISBN (International Standard Book Number), the International Standard Book Number, is a consecutive number used to identify books. It consists of ten or thirteen digits and is verified by certain calculations to ensure its correctness. In this article, we will introduce how to use PHP regular expressions to verify that the input string is in the correct ISBN encoding format.

ISBN encoding format

ISBN encoding consists of 10 or 13 digits, the first 9 digits are the book identification code, and the 10th digit is the check code. For the 13-digit ISBN code, the first three digits are the country or region code, followed by the publisher's identification code, then the book's identification code, and finally the check code. For the 10-digit ISBN code, the first 9 digits are also the book identification code, but there is no country or region code and publisher identification code, and the last digit is still a check code.

Verification rules

The verification rules are as follows:

For the 10-digit ISBN code: the check code is the last digit, and the calculation rules are as follows: add the first 9 digits Multiply the weight coefficients from 1 to 9 respectively, and then add these 9 products to get a total. Then take the remainder of this sum to 11, and subtract the remainder from 11 to get the check code. If the remainder is 10, the check code is a capital letter X.

For 13-digit ISBN encoding: the check code is the last digit, and the calculation rules are as follows: multiply the first 12 digits by a weight coefficient of 1 or 3 respectively, and then add these 13 products to get a sum. Then take the remainder of this sum to 10, and then subtract the remainder from 10 to get the check code.

PHP Regular Expression

The following is the code for PHP regular expression, which is used to verify whether the input string is in the correct ISBN encoding format.

For 10-digit ISBN code:

function checkISBN10($isbn) {
    if (!preg_match('/^d{9}[d|X]$/i', $isbn)) {
        return false;
    }
    $checkbit = 0;
    for ($i = 0; $i < 9; $i++) {
        $checkbit += intval($isbn{$i}) * (10 - $i);
    }
    $checkbit = ($checkbit % 11 == 0) ? 0 : 11 - ($checkbit % 11);
    return ($isbn{9} == ($checkbit == 10 ? 'X' : strval($checkbit)));
}

For 13-digit ISBN code:

function checkISBN13($isbn) {
    if (!preg_match('/^d{13}$/i', $isbn)) {
        return false;
    }
    $checkbit = 0;
    for ($i = 0; $i < 12; $i++) {
        $checkbit += intval($isbn{$i}) * (($i % 2 == 0) ? 1 : 3);
    }
    $checkbit = 10 - ($checkbit % 10);
    return ($isbn{12} == strval($checkbit));
}

These two functions accept an ISBN code as input and return a Boolean value, Indicates whether the code is a legal ISBN code. If it is a legal encoding, it returns true, otherwise it returns false.

These functions use regular expressions to check whether the input conforms to the ISBN encoding format. Then use the verification rules to calculate the check code and compare it with the check code in the input encoding to determine whether the encoding is legal.

Conclusion

This article introduces how to use PHP regular expressions to verify whether the input string is in the correct ISBN encoding format. Although the verification rules of ISBN encoding are relatively complex, with the help of the powerful function of regular expressions, we can easily implement this verification. Developers can use these functions to check the ISBN code entered by the user to ensure its correctness.

The above is the detailed content of PHP regular expression to verify that the input string is in the correct ISBN encoding format. 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