Home Backend Development PHP Tutorial PHP introductory tutorial (20) Commonly used regular expressions in PHP

PHP introductory tutorial (20) Commonly used regular expressions in PHP

Jul 25, 2016 am 08:55 AM

This article is the twentieth section of the PHP introductory tutorial. It introduces some commonly used regular expressions in PHP. Interested friends can refer to it.

Content of this section; Commonly used regular expressions in php

Regular expression matching Chinese characters: [u4e00-u9fa5] Comment: Matching Chinese is really a headache. With this expression, it will be easier

Match double-byte characters (including Chinese characters): [^x00-xff] Comment: Can be used to calculate the length of a string (the length of a double-byte character counts as 2, and the length of an ASCII character counts as 1)

Regular expression matching blank lines: ns*r Comment: Can be used to delete blank lines

Regular expression matching HTML tags: ]*>.*?1>|<.> Comment: The version circulating on the Internet is too bad. The above one can only match part of it, and it is still powerless for complex nested tags

Regular expression matching leading and trailing whitespace characters: ^s*|s*$ Comment: It can be used to delete whitespace characters (including spaces, tabs, form feeds, etc.) at the beginning and end of the line. It is a very useful expression

Regular expression matching email addresses: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* Comment: Very useful for form verification

Regular expression matching URL: [a-zA-z]+://[^s]* Comment: The version circulating on the Internet has very limited functions. The above one can basically meet the needs

Whether the matching account is legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ Comment: Very useful for form verification

Matching domestic phone numbers: d{3}-d{8}|d{4}-d{7} Comment: Matching format is such as 0511-4405222 or 021-87888822

Match Tencent QQ number: [1-9][0-9]{4,} Comment: Tencent QQ account starts from 10000

Match Chinese postal code: [1-9]d{5}(?!d) Comment: China’s postal code is a 6-digit number

Matching ID card: d{15}|d{18} Comment: China’s ID card has 15 or 18 digits

Matching ip address: d+.d+.d+.d+ Comment: Useful when extracting IP address

Match specific numbers: ^[1-9]d*$ //Match positive integers ^-[1-9]d*$ //Match negative integers ^-?[1-9]d*$ //Match integers ^[1-9]d*|0$ // Match non-negative integers (positive integers + 0) ^-[1-9]d*|0$ //Match non-positive integers (negative integers + 0) ^[1-9]d*.d*|0.d*[1-9]d*$ //Match positive floating point numbers ^-([1-9]d*.d*|0.d*[1-9]d*)$ //Match negative floating point numbers ^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ // Match floating point numbers ^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ //Match non-negative floating point numbers (positive floating point numbers + 0) ^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$  //Match non-positive floating point numbers (negative floating point numbers + 0 ) Comment: Useful when processing large amounts of data, please pay attention to corrections when applying it

Match specific string: ^[A-Za-z]+$  //Match a string consisting of 26 English letters ^[A-Z]+$ // Matches a string consisting of 26 uppercase English letters ^[a-z]+$ //Match a string consisting of 26 lowercase English letters ^[A-Za-z0-9]+$  // Matches a string consisting of numbers and 26 English letters ^w+$  // Matches a string consisting of numbers, 26 English letters, or underscores Comment: Some of the most basic and commonly used expressions



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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles