PHP正则表达式的几则使用技巧
PHP正则表达式主要用于字符串的模式分割、匹配、查找及替换操作。使用正则表达式在某些简单的环境下可能效率不高,因此如何更好的使用PHP正则表达式需要综合考虑
我的PHP正则入门,是起源于网上的一篇文章,这篇文章由浅入深的阐述了PHP正则表达式使用的方法,我觉得是一个很好的入门材料,不过学成还是要靠个人,在使用的过程中,还是会不断地忘记,因此反反复复的阅读了这篇文章有四五遍,对于其中一些比较困难的知识点,甚至要用很久才能消化,但是只要能见坚持着看完,你会发现自己对于正则的运用能力就会显著提高。BKJIA编辑推荐《PHP开发基础入门》
PHP正则表达式的定义:
用于描述字符排列和匹配模式的一种语法规则。它主要用于字符串的模式分割、匹配、查找及替换操作。
PHP中的正则函数:
PHP中有两套正则函数,两者功能差不多,分别为:
一套是由PCREPerl Compatible Regular Expression)库提供的。使用“preg_”为前缀命名的函数;
一套由POSIXPortable Operating System Interface of Unix )扩展提供的。使用以“ereg_”为前缀命名的函数;POSIX的正则函数库,自PHP 5.3以后,就不在推荐使用,从PHP6以后,就将被移除)
由于POSIX正则即将推出历史舞台,并且PCRE和perl的形式差不多,更利于我们在perl和php之间切换,所以这里重点介绍PCRE正则的使用。
PCRE正则表达式
PCRE全称为Perl Compatible Regular Expression,意思是Perl兼容正则表达式。
在PCRE中,通常将模式表达式即正则表达式)包含在两个反斜线“/”之间,如“/apple/”。
正则中重要的几个概念有:元字符、转义、模式单元重复)、反义、引用和断言,这些概念都可以在文章[1]中轻松的理解和掌握。
常用的元字符(Meta-character):
元字符 说明
\A 匹配字符串串首的原子
\Z 匹配字符串串尾的原子
\b 匹配单词的边界 /\bis/ 匹配头为is的字符串 /is\b/ 匹配尾为is的字符串 /\bis\b/ 定界
\B 匹配除单词边界之外的任意字符 /\Bis/ 匹配单词“This”中的“is”
\d 匹配一个数字;等价于[0-9]
\D 匹配除数字以外任何一个字符;等价于[^0-9]
\w 匹配一个英文字母、数字或下划线;等价于[0-9a-zA-Z_]
\W 匹配除英文字母、数字和下划线以外任何一个字符;等价于[^0-9a-zA-Z_]
\s 匹配一个空白字符;等价于[\f\t\v]
\S 匹配除空白字符以外任何一个字符;等价于[^\f\t\v]
\f 匹配一个换页符等价于 \x0c 或 \cL
匹配一个换行符;等价于 \x0a 或 \cJ
匹配一个回车符等价于\x0d 或 \cM
\t 匹配一个制表符;等价于 \x09\或\cl
\v 匹配一个垂直制表符;等价于\x0b或\ck
\oNN 匹配一个八进制数字
\xNN 匹配一个十六进制数字
\cC 匹配一个控制字符
模式修正符Pattern Modifiers):
模式修正符在忽略大小写、匹配多行中使用特别多,掌握了这一个修正符,往往能解决我们遇到的很多问题。
i -可同时匹配大小写字母
M -将字符串视为多行
S -将字符串视为单行,换行符做普通字符看待,使“.”匹配任何字符
X -模式中的空白忽略不计
U -匹配到最近的字符串
e -将替换的字符串作为表达使用
格式:/apple/i匹配“apple”或“Apple”等,忽略大小写。 /i
PCRE的模式单元:
//1 提取第一位的属性
/^\d{2} ([\W])\d{2}\\1\d{4}$匹配“12-31-2006”、“09/27/1996”、“86 01 4321”等字符串。但上述正则表达式不匹配“12/34-5678”的格式。这是因为模式“[\W]”的结果“/”已经被存储。下个位置“\1”引用时,其匹配模式也是字符“/”。
当不需要存储匹配结果时使用非存储模式单元“?:)”
例如/(?:a|b|c)(D|E|F)\\1g/ 将匹配“aEEg”。在一些正则表达式中,使用非存储模式单元是必要的。否则,需要改变其后引用的顺序。上例还可以写成/a|b|c)(C|E|F)\2g/。
PCRE正则表达式函数:
<ol class="dp-c"> <li class="alt"><span><span>preg_match()和preg_match_all() </span></span></li> <li class=""><span>preg_quote() </span></li> <li class="alt"><span>preg_split() </span></li> <li class=""><span>preg_grep() </span></li> <li class="alt"><span>preg_replace() </span></li> </ol>
函数的具体使用,我们可以通过PHP手册来找到,下面分享一些平时积累的正则表达式:
匹配action属性
<ol class="dp-c"> <li class="alt"><span><span class="vars"><font color="#dd0000">$str</font></span><span> = </span><span class="string"><font color="#0000ff">'<form></form> <form test.php www.bac.com></form> <form>'</form></font></span><span>; </span></span></li> <li class=""> <span> </span><span class="vars"><font color="#dd0000">$match</font></span><span> = </span><span class="string"><font color="#0000ff">''</font></span><span>; </span> </li> <li class="alt"> <span> preg_match_all(</span><span class="string"><font color="#0000ff">'/\s+action=\"(?!http:)(.*?)\"\s/'</font></span><span>, </span><span class="vars"><font color="#dd0000">$str</font></span><span>, </span><span class="vars"><font color="#dd0000">$match</font></span><span>); </span> </li> <li class=""> <span> print_r(</span><span class="vars"><font color="#dd0000">$match</font></span><span>); </span> </li> </ol>
在正则中使用回调函数
<ol class="dp-c"> <li class="alt"><span><span class="comment"><font color="#008200">/** </font></span> </span></li> <li class=""><span><span class="comment"><font color="#008200"> * replace some string by callback function </font></span> </span></li> <li class="alt"><span><span class="comment"><font color="#008200"> * </font></span> </span></li> <li class=""><span><span class="comment"><font color="#008200"> */</font></span><span> </span></span></li> <li class="alt"> <span> </span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> callback_replace() { </span> </li> <li class=""> <span> </span><span class="vars"><font color="#dd0000">$url</font></span><span> = </span><span class="string"><font color="#0000ff">'http://esfang.house.sina.com.cn'</font></span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars"><font color="#dd0000">$str</font></span><span> = </span><span class="string"><font color="#0000ff">'<form></form> <form test.php www.bac.com></form> <form>'</form></font></span><span>; </span> </li> <li class=""> <span> </span><span class="vars"><font color="#dd0000">$str</font></span><span> = preg_replace ( </span><span class="string"><font color="#0000ff">'/(?</font></span><span>, </span><span class="string"><font color="#0000ff">'search(\$url, \\1)'</font></span><span>, </span><span class="vars"><font color="#dd0000">$str</font></span><span> ); </span> </li> <li class="alt"><span> </span></li> <li class=""> <span> </span><span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$str</font></span><span>; </span> </li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"> <span> </span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> search(</span><span class="vars"><font color="#dd0000">$url</font></span><span>, </span><span class="vars"><font color="#dd0000">$match</font></span><span>){ </span> </li> <li class=""> <span> </span><span class="keyword"><strong><font color="#006699">return</font></strong></span><span> </span><span class="vars"><font color="#dd0000">$url</font></span><span> . </span><span class="string"><font color="#0000ff">'/'</font></span><span> . </span><span class="vars"><font color="#dd0000">$match</font></span><span>; </span> </li> <li class="alt"><span> } </span></li> </ol>
带断言的正则匹配
<ol class="dp-c"> <li class="alt"><span><span class="vars"><font color="#dd0000">$match</font></span><span> = </span><span class="string"><font color="#0000ff">''</font></span><span>; </span></span></li> <li class=""> <span> </span><span class="vars"><font color="#dd0000">$str</font></span><span> = </span><span class="string"><font color="#0000ff">'xxxxxx.com.cn <b>bold font</b> <p>paragraph text</p>'</font></span><span>; </span> </li> <li class="alt"> <span> preg_match_all ( </span><span class="string"><font color="#0000ff">'/(?).*(?=)/'</font></span><span>, </span><span class="vars"><font color="#dd0000">$str</font></span><span>, </span><span class="vars"><font color="#dd0000">$match</font></span><span> ); </span> </li> <li class=""> <span> </span><span class="func">echo</span><span> </span><span class="string"><font color="#0000ff">"<br>匹配没有属性的HTML标签中的内容:"</font></span><span>; </span> </li> <li class="alt"> <span> print_r ( </span><span class="vars"><font color="#dd0000">$match</font></span><span> ); </span> </li> </ol>
替换HTML源码中的地址
<ol class="dp-c"><li class="alt"><span><span class="vars"><font color="#dd0000">$form_html</font></span><span> = preg_replace ( </span><span class="string"><font color="#0000ff">'/(?</font></span><span>, </span><span class="string"><font color="#0000ff">'add_url(\$url, \'\\1\')'</font></span><span>, </span><span class="vars"><font color="#dd0000">$form_html</font></span><span> ); </span></span></li></ol>
最后,正则工具虽然强大,但是从效率和编写时间上来讲,有的时候可能没有explode来的更直接,对于一些紧急或者要求不高的任务,简单、粗暴的方法也许更好。
而对于preg和ereg两个系列之间的执行效率,曾看到文章说preg要更快一点,具体由于使用ereg的时候并不多,而且也要推出历史舞台了,再加个个人更偏好于PCRE的方式,所以笔者就不做比较了,熟悉的朋友可以发表下意见,谢谢。
本文来自Cocowool的博客园博文《PHP中正则的使用》
- 如何在PHP中使用正则表达式
- 在PHP中使用与Perl兼容的正则表达式
- 正则表达式在网页处理中的应用四则
- 论Web2.0时代的PHP:优点还是问题?
- 开源语言排行榜:PHP与JavaScript受青睐

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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,

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

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

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.

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.
