search
HomeBackend DevelopmentPHP TutorialPHP下常用正则表达式整理_PHP

正则表达式

--------------------------------------------------------- 正则收藏

手机号码:
$mode = "/^1[358]\d{9}/";

邮箱地址:
$mode = "/^[a-z][-_\.]?[a-z\d]*@[a-z0-9]+[\.][a-z]{2,4}/i";

---------------------------------------------------------- 正则基础

$mode = "/^1[358]\d{9}/i";
匹配模块必须以 / / 开始和结束,第二个 / 后可以加模式修正符

原子
①a-z A-Z _ 0-9 //最常见的字符
②(abc) //用圆括号括起来起来的单元符号
③[abcs] [^abd] //用方括号括起来的原子表,
原子表中的^代表排除或相反内容

\d 包含所有数字[0-9]
\D 除所有数字外[^0-9]
\w 包含所有英文字符[a-zA-Z_0-9]
\W 除所有英文字符外[^a-zA-Z_0-9]
\s 包含空白区域如回车、换行、分页等 [\f\n\r]

元字符
* 匹配前一个内容的0次1次或多次
+ 1次或多次
? 0次或1次
. 代表任意一个字符(除了回车换行)
| 相当与php的 || (“或”的意思)
^ 强制匹配字符串首部内容
$ 强制匹配字符串尾部内容
[^abc] 匹配除了a或b或c之外的内容
\b 匹配单词边界,边界可以是空格或者特殊符号
\B 匹配除带单词边界以外的内容
{m} 匹配前一个内容的重复次数为M次
{m,} 匹配前一个内容的重复次数大于等于M次
{m,n} 匹配前一个内容的重复次数M次到N次
( ) 整体匹配,并放入内存,可使用\\1 或 \\2 …依次获取

优先级:依次降低
( ) 圆括号因为是内存处理所以最高
* ? + { } 重复匹配内容其次
^ $ \b 边界处理第三
| 条件处理第四
最后按照运算顺序计算匹配

常用修正符: $mode = "/正则/U";
i 正则内容在匹配时候不区分大小写(默认是区分的)
m 在匹配首内容或者尾内容时候采用多行识别匹配
S 将回车转化为空格
x 忽略正则中的空白
A 强制从头开始匹配
D 强制$匹配尾部无任何内容 \n
U 禁止贪婪匹配,只跟踪到最近的一个匹配符并结束,
常用在采集程序上的正则表达式

应用
preg_match_all ( string pattern, string subject, array matches [, int flags] )
截取比较详细的内容,采集网页,分析文本
preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
提示 1、替换内容可以是一个正则也可以是数组正则
2、替换内容可以通过修正符e来解决替换执行内容
preg_split ( string pattern, string subject [, int limit [, int flags]] )
通过正则表达式来切割相关内容,类似之前学过的explode切割函数,但explode
只能按照一种方式切割有局限性。
------------------------------------------------- 调试代码
[code]
$mode = "/^[a-z][-_\.]?[a-z\d]*@[a-z0-9]+[\.][a-z]{2,4}/i";
$str = "a12345@bitsCN.com";
echo $str.'
';
if(preg_match($mode, $str, $arr)){
echo 'succeed -- '.$arr[0];
}else{
echo 'failed';
}
?>
[code]
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
How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

How can you trace session activity in PHP?How can you trace session activity in PHP?Apr 27, 2025 am 12:10 AM

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

How can you use a database to store PHP session data?How can you use a database to store PHP session data?Apr 27, 2025 am 12:02 AM

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

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 Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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