


Detailed explanation of PHP preg_match regular expression_PHP tutorial
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

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

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,

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

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

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.

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.
