search
HomeBackend DevelopmentPHP TutorialPHP introductory tutorial (20) Commonly used regular expressions in PHP

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
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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 Article

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools