Home  >  Article  >  Backend Development  >  PHP regular expression to verify whether the input string is in the correct corporate registration number or organization code format

PHP regular expression to verify whether the input string is in the correct corporate registration number or organization code format

王林
王林Original
2023-06-24 08:22:051341browse

With 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.

//验证企业注册号
function check_registered_no($str) {
    $pattern = '/^[a-zA-Z0-9]{15}([a-zA-Z0-9]{3})?$/';
    return preg_match($pattern, $str);
}

//验证组织机构代码
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'; // 企业注册号
if(check_registered_no($str)){
    echo '企业注册号格式正确';
}else{
    echo '企业注册号格式错误';
}

$str = '914403003351571282-G1'; // 组织机构代码
if(check_organization_code($str)){
    echo '组织机构代码格式正确';
}else{
    echo '组织机构代码格式错误';
}

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!

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