


PHP regular expression to verify whether the input password contains numbers, letters and symbols
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:
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~
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)) { // 包含数字 }
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)) { // 包含字母 }
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)) { // 包含符号 }
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; }
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!

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

AI Hentai Generator
Generate AI Hentai for free.

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

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

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

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

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

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

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

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

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