Home Backend Development PHP Tutorial 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

Jun 24, 2023 am 08:22 AM
php regular expression Enterprise registration number

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})?$/';
Copy after login

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]$/';
Copy after login

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);
}
Copy after login

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 '组织机构代码格式错误';
}
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles