


PHP regular expression to verify that the input string is in the correct ISBN encoding format
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!

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
