PHP preg_match正则表达式详解
PHP正则表达式规则匹配是什么呢?这就是PHP preg_match正则表达式的使用,那么对于PHP preg_match正则表达式我们需要注意什么呢?这里向你通过实例的使用向你详细介绍。
PHP preg_match正则表达式的使用:
利用 preg_match(),我们可以完成字符串的规则匹配。如果找到一个匹配,preg_match() 函数返回 1,否则返回 0。还有一个可选的第三参数可以让你把匹配的部分存在一个数组中。在验证数据时这个功能可以变得非常有用 SUN JAVA Certification
<ol class="dp-c"> <li class="alt"><span><span class="vars">$string</span><span> = </span><span class="string">"football"</span><span>; </span></span></li> <li> <span class="keyword">if</span><span> (preg_match(</span><span class="string">'/foo/'</span><span>, </span><span class="vars">$string</span><span>)) { </span> </li> <li class="alt"> <span class="comment">// 匹配正确 </span><span> </span> </li> <li><span>} </span></li> </ol>
上面的例子将成功匹配,因为单词 football 里面包含 foo。现在我们来试一个更复杂的,例如验证一个 Email 地址。
<ol class="dp-c"> <li class="alt"><span><span class="vars">$string</span><span> = </span><span class="string">"first.last@domain.uno.dos"</span><span>; </span></span></li> <li> <span class="keyword">if</span><span> (preg_match( </span> </li> <li class="alt"><span>'/^[^0-9][a-zA-Z0-9_]+ </span></li> <li><span>([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+ </span></li> <li class="alt"><span>([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', </span></li> <li> <span class="vars">$string</span><span>)) { </span> </li> <li class="alt"> <span class="comment">// 验证Email地址 </span><span> </span> </li> <li><span>} </span></li> </ol>
这个例子将验证出此 Email 地址为正确格式。
PHP preg_match正则表达式的规则:
通过上面实例的演示我们来了解下这段正则表达式所代表的各种规则。
PCRE 顾名思义,与在 Perl 中的正则表达式有相同的语法,所以每段正则表达式必须要有一对定界符。我们一般使用 / 为定界符。
开头的 ^ 和结尾的 $ 让PHP从字符串开头检查到结尾。假使没有 $,程序仍会匹配到 Email 的末尾。
◆[ 和 ] 被用来限制许可输入类型。例如 a-z 允许所有的小写字母,A-Z 允许所有的大写字母,0-9 所有数字,等等,以及更多其他类型。
◆{ 和 } 被用来限制期望的字符数。例如 {2,4} 表示字符串的每一节可以有 2-4 字符长度,像是 .com.cn 或 .info。在这里, "." 并不算一个字符,因为 {2,4} 之前定义的许可输入类型只有大小写字母,故此段只匹配大小写字母
◆( 和 ) 被用来合并小节,并定义字符串中必须存在的字符。(a|b|c) 能够匹配 a 或 b 或 c。
◆(.) 将匹配所有字符,而 [.] 只匹配 "." 本身。
要使用一些符号本身,必须在前增加一个 。这些字符有:( ) [ ] . * ? + ^ | $
PHP preg_match正则表达式的相关内容就向你介绍到这里,希望对你了解和掌握PHP preg_match正则表达式有所帮助。

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

AI Hentai Generator
Generate AI Hentai for free.

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

Validator can be created by adding the following two lines in the controller.
