php中常用正则表达式总结
本文章收集了在php中一些常常用到的正则表达式规则总结,有需要的朋友可参考。
匹配特定数字:
代码如下 | 复制代码 |
^[1-9]d*$ //匹配正整数 |
评注:处理大量数据时有用,具体应用时注意修正
匹配特定字符串:
代码如下 | 复制代码 |
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串 |
匹配中文字符的正则表达式: [u4e00-u9fa5]
例
代码如下 | 复制代码 |
$str = "php编程"; |
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了
匹配双字节字符(包括汉字在内):[^x00-xff]
例
代码如下 | 复制代码 |
$str = "singlepoint单点日志";
} |
评注:编码表 双字节字符编码范围 1. GBK (GB2312/GB18030) x00-xff GBK双字节编码范围 x20-x7f ASCII
xa1-xff 中文 gb2312 x80-xff 中文 gbk 2. UTF-8 (Unicode) u4e00-u9fa5 (中文) x3130-x318F (韩文
xAC00-xD7A3 (韩文) u0800-u4e00 (日文)
匹配空白行的正则表达式:ns*r
代码如下 | 复制代码 |
$str = "123 456"; |
评注:可以用来删除空白行
匹配HTML标记的正则表达式:]*>.*?1>|<.>
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
匹配首尾空白字符的正则表达式:^s*|s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式
匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
评注:表单验证时很实用
匹配网址URL的正则表达式:[a-zA-z]+://[^s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注:表单验证时很实用
匹配国内电话号码:d{3}-d{8}|d{4}-d{7}
评注:匹配形式如 0511-4405222 或 021-87888822
匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10000开始
匹配中国邮政编码:[1-9]d{5}(?!d)
评注:中国邮政编码为6位数字
匹配身份证:d{15}|d{18}
评注:中国的身份证为15位或18位
匹配ip地址:d+.d+.d+.d+
评注:提取ip地址时有用
可能对你有用的与正则表达式有关的内容
一,a-z A-Z_0-9 //最常见的字符
二,(bfw)(sda) //用圆括号包含起来的单元符号,一个括号表示一个整体
三,[sdwe][^mjnb] //用方括号包含起来的原子表,原子表中^代表排除或相反内容
四,转义字符
d 包含所有的数字[0-9]
D 除所有数字外[^0-9]
w 包含所有英文字符[a-zA-Z_0-9]
W 除所有英文字符外[^a-zA-Z_0-9] -----匹配特殊字符
s 包含空白区域 如回车,换行,分页等[fnr]
4,正则表达式元字符
* 匹配前一个内容的0次或多次
. 匹配内容的0次或多次,但不包含回车换行
+ 匹配前一个内容的1次或多次
? 匹配前一个内容的0次或1次
| 选择匹配,类似php中||的用法
^ 匹配字符串首部的内容
$ 匹配字符串尾部内容
b 匹配单词边界,边界可以是空格或者特殊符号
B 匹配除带单词边界的意外内容
{m} 匹配前一个内容的重复次数为m次
{m,} 匹配前一个内容的重复次数大于等于m次
{m,n} 匹配前一个内容的重复次数m次到n次
() 合并整体匹配,并放入内存,可使用\1\2依次获取调用

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











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

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 and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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 still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
