


PHP regular expression to verify whether the input string is in the correct corporate registration number or organization code format
Jun 24, 2023 am 08:22 AMWith the rapid development of the Internet, network security issues have attracted more and more people's attention. Among them, the security of enterprise registration numbers and organization codes is also increasingly important. In order to ensure the security of corporate information, PHP's regular expressions have become a powerful tool for verifying corporate registration numbers and organization code formats.
First of all, we need to understand the definition and format of the enterprise registration number and organization code. The enterprise registration number refers to the unique number registered by the enterprise with the industrial and commercial administration department. It consists of numbers and letters, and the format is 15 or 18 digits. Organization code refers to the code used by organizations in legal, administrative and policy matters. It consists of numbers and letters and has a 9-digit format.
Next, we will explain how to use PHP regular expressions to verify the company registration number and organization code.
First of all, we need to define two regular expressions, which are the verification rules for the enterprise registration number and the organization code.
The verification rules for enterprise registration numbers are as follows:
$pattern = '/^[a-zA-Z0-9]{15}([a-zA-Z0-9]{3})?$/';
w
in the above regular expression represents numbers and letters, {15}
represents the length It is 15 digits, ([a-zA-Z0-9]{3})?
represents an optional 3-digit suffix.
The verification rules of the organization code are as follows:
$pattern = '/^[a-zA-Z0-9]{8}-[a-zA-Z0-9]$/';
The -
symbols in the above regular expression represent the separator in the middle of the organization code.
Next, we will use the preg_match()
function in PHP to verify the company registration number and organization code.
//驗證企業(yè)注冊號 function check_registered_no($str) { $pattern = '/^[a-zA-Z0-9]{15}([a-zA-Z0-9]{3})?$/'; return preg_match($pattern, $str); } //驗證組織機構(gòu)代碼 function check_organization_code($str) { $pattern = '/^[a-zA-Z0-9]{8}-[a-zA-Z0-9]$/'; return preg_match($pattern, $str); }
The above functions are verification functions for enterprise registration number and organization code respectively. When the input string matches the corresponding regular expression, 1 is returned, otherwise 0 is returned.
In practical applications, we can use the following code to use the above functions to verify the corresponding format.
$str = '913301006071937303'; // 企業(yè)注冊號 if(check_registered_no($str)){ echo '企業(yè)注冊號格式正確'; }else{ echo '企業(yè)注冊號格式錯誤'; } $str = '914403003351571282-G1'; // 組織機構(gòu)代碼 if(check_organization_code($str)){ echo '組織機構(gòu)代碼格式正確'; }else{ echo '組織機構(gòu)代碼格式錯誤'; }
Through the above code, we can easily verify whether the entered enterprise registration number and organization code comply with the specifications, and give corresponding result prompts.
In short, PHP's regular expression verification method can provide us with a fast and accurate verification method of corporate registration number and organization code format, effectively ensuring the security of corporate information.
The above is the detailed content of PHP regular expression to verify whether the input string is in the correct corporate registration number or organization code format. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Comments cannot be careless because they want to explain the reasons for the existence of the code rather than the functions, such as compatibility with old interfaces or third-party restrictions, otherwise people who read the code can only rely on guessing. The areas that must be commented include complex conditional judgments, special error handling logic, and temporary bypass restrictions. A more practical way to write comments is to select single-line comments or block comments based on the scene. Use document block comments to explain parameters and return values at the beginning of functions, classes, and files, and keep comments updated. For complex logic, you can add a line to the previous one to summarize the overall intention. At the same time, do not use comments to seal code, but use version control tools.

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

The key to setting up PHP is to clarify the installation method, configure php.ini, connect to the web server and enable necessary extensions. 1. Install PHP: Use apt for Linux, Homebrew for Mac, and XAMPP recommended for Windows; 2. Configure php.ini: Adjust error reports, upload restrictions, etc. and restart the server; 3. Use web server: Apache uses mod_php, Nginx uses PHP-FPM; 4. Install commonly used extensions: such as mysqli, json, mbstring, etc. to support full functions.

The key to writing PHP comments is clear, useful and concise. 1. Comments should explain the intention behind the code rather than just describing the code itself, such as explaining the logical purpose of complex conditional judgments; 2. Add comments to key scenarios such as magic values, old code compatibility, API interfaces, etc. to improve readability; 3. Avoid duplicate code content, keep it concise and specific, and use standard formats such as PHPDoc; 4. Comments should be updated synchronously with the code to ensure accuracy. Good comments should be thought from the perspective of others, reduce the cost of understanding, and become a code understanding navigation device.

PHPblockcommentsareusefulforwritingmulti-lineexplanations,temporarilydisablingcode,andgeneratingdocumentation.Theyshouldnotbenestedorleftunclosed.BlockcommentshelpindocumentingfunctionswithPHPDoc,whichtoolslikePhpStormuseforauto-completionanderrorche

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

The first step is to select the integrated environment package XAMPP or MAMP to build a local server; the second step is to select the appropriate PHP version according to the project needs and configure multiple version switching; the third step is to select VSCode or PhpStorm as the editor and debug with Xdebug; in addition, you need to install Composer, PHP_CodeSniffer, PHPUnit and other tools to assist in development.
