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@jb51.net";
echo $str.'
';
if(preg_match($mode, $str, $arr)){
echo 'succeed -- '.$arr[0];
}else{
echo 'failed';
}
?>
[code]

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

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

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











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

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,

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.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
