Home Backend Development PHP Tutorial PHP regular expression to verify whether the input password contains numbers, letters and symbols

PHP regular expression to verify whether the input password contains numbers, letters and symbols

Jun 24, 2023 am 11:21 AM
php regular expression Password validation

In the field of computer programming, regular expressions are a powerful tool that can greatly simplify data matching and processing. When developing a website or application that requires user registration, in order to ensure the security of the password, it is often necessary to verify the entered password. The best way is to use regular expressions.

As a popular server-side programming language, PHP has a built-in regular expression function library, which can easily verify the input password. In this article, we will introduce how to use PHP regular expressions to verify whether the entered password contains numbers, letters, and symbols.

First, we need to define a password rule, which stipulates that the password must contain at least one number, at least one letter, and at least one symbol. The following is the definition of symbols, including various commonly used symbols:

! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [  ] ^ _ ` { | } ~
Copy after login

Next, we will use PHP's regular expression function preg_match() to verify password rules. preg_match()The function is used to match regular expressions and return matching results. For example, the following code can be used to check whether a string contains a number:

if (preg_match('/d/', $password)) {
    // 包含数字
}
Copy after login

where d is a special character representing a number in a regular expression. Similarly, the following code can be used to check whether a string contains letters:

if (preg_match('/[a-zA-Z]/', $password)) {
    // 包含字母
}
Copy after login

where [a-zA-Z] represents any letter, including uppercase and lowercase letters. We can use [a-z] to include only lowercase letters, or [A-Z] to include only uppercase letters.

Finally, we need to check if the string contains symbols. There are many symbols, and some symbols in regular expressions need to be escaped to match. The following code can be used to check whether a string contains symbols:

if (preg_match('/[^a-zA-Z0-9]/', $password)) {
    // 包含符号
}
Copy after login

Where, [^a-zA-Z0-9] means any character except letters and numbers, use [^...] means excluding certain characters.

By combining the above codes, we can easily write a PHP function to verify whether the entered password complies with the rules:

function validate_password($password) {
    // 密码包含数字
    if (!preg_match('/d/', $password)) {
        return false;
    }
    // 密码包含字母
    if (!preg_match('/[a-zA-Z]/', $password)) {
        return false;
    }
    // 密码包含符号
    if (!preg_match('/[^a-zA-Z0-9]/', $password)) {
        return false;
    }
    // 密码长度至少为8位
    if (strlen($password) < 8) {
        return false;
    }
    return true;
}
Copy after login

This function will also check whether the password contains numbers and letters and symbols, and the password must be at least 8 characters long. If the rules are not met, the function returns false, otherwise it returns true.

In practical applications, we can use the above function in combination with the user registration function, so as to ensure the security of the password entered by the user and greatly reduce the risk of being attacked or illegally accessed.

The above is the detailed content of PHP regular expression to verify whether the input password contains numbers, letters and symbols. 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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

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

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

See all articles