前面的话
正则表达式不能独立使用,它只是一种用来定义字符串的规则模式,必须在相应的正则表达式函数中应用,才能实现对字符串的匹配、查找、替换及分割等操作。前面介绍了正则表达式的基础语法,本文将详细介绍正则表达式函数
匹配与查找
【preg_match()】
preg_match()函数用来执行一个正则表达式匹配,搜索subject与pattern给定的正则表达式的一个匹配。返回pattern的匹配次数。它的值将是0次(不匹配)或1次,因为preg_match()在第一次匹配后将会停止搜索。preg_match_all()不同于此,它会一直搜索subject直到到达结尾。如果发生错误preg_match()返回FALSE
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
pattern表示要搜索的模式,字符串类型
subject表示输入字符串
如果提供了参数matches,它将被填充为搜索结果。$matches[0]将包含完整模式匹配到的文本, $matches[1] 将包含第一个捕获子组匹配到的文本,以此类推
flags可以被设置为以下标记:1、PREG_OFFSET_CAPTURE。如果传递了这个标记,对于每一个出现的匹配返回时会附加字符串偏移量(相对于目标字符串的)。注意:这会改变填充到matches参数的数组,使其每个元素成为一个由第0个元素是匹配到的字符串,第1个元素是该匹配字符串在目标字符串subject中的偏移量;2、offset。通常,搜索从目标字符串的开始位置开始。可选参数offset用于指定从目标字符串的某个未知开始搜索(单位是字节)
<?php //从URL中获取主机名称 preg_match('@^(?:http://)?([^/]+)@i', "http://www.php.net/index.html", $matches); $host = $matches[1];//获取主机名称的后面两部分 preg_match('/[^.]+.[^.]+$/', $host, $matches);//domain name is: php.netecho "domain name is: {$matches[0]}"; ?>
<?php $pattern = '/www.[^./]+.com/i'; $subject = 'www.baidu.com,www.qq.com,www.cnblogs.com';preg_match($pattern,$subject,$matches);/*array (size=1) 0 => string 'www.baidu.com' (length=13) */var_dump($matches); ?>
【preg_match_all()】
preg_match_all()与preg_match()类似,不同的是preg_match()在第一次匹配之后就会停止搜索,而函数preg_match_all()则会一直搜索到指定字符串的结尾,可以获取到所有匹配到的结果
int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
<?php $pattern = '/www.[^./]+.com/i';$subject = 'www.baidu.com,www.qq.com,www.cnblogs.com'; preg_match_all($pattern,$subject,$matches); /* array (size=1) 0 => array (size=3) 0 => string 'www.baidu.com' (length=13) 1 => string 'www.qq.com' (length=10) 2 => string 'www.cnblogs.com' (length=15) */ var_dump($matches); ?>
【preg_grep()】
preg_grep()返回给定数组input中与模式pattern 匹配的元素组成的数组
array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )
如果flags设置为PREG_GREP_INVERT,这个函数返回输入数组中与 给定模式pattern不匹配的元素组成的数组
<?php $pattern = '/www.[^./]+.com/i'; $subject = ['baidu.com','www.qq.com','www.cnblogs.com'];var_dump (preg_grep($pattern,$subject)); ?>
替换
【preg_replace()】
preg_replace()执行一个正则表达式的搜索替换,搜索subject匹配pattern的部分,以replacement进行替换 mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
replacement表示用于替换的字符串或字符串数组。如果这个参数是一个字符串,并且pattern是一个数组,那么所有的模式都使用这个字符串进行替换。如果pattern和replacement都是数组,每个pattern使用replacement中对应的元素进行替换。如果replacement中的元素比pattern中的少,多出来的pattern使用空字符串进行替换
<?php $string = 'April 15, 2016';$pattern = '/(w+) (d+), (d+)/i'; $replacement = '${1}1,$3';//April1,2016echo preg_replace($pattern, $replacement, $string); ?><?php $string = 'The quick brown fox jumped over the lazy dog.'; $patterns = array(); $patterns[0] = '/quick/'; $patterns[1] = '/brown/'; $patterns[2] = '/fox/';$replacements = array(); $replacements[2] = 'bear';$replacements[1] = 'black'; $replacements[0] = 'slow';//The bear black slow jumped over the lazy dog.echo preg_replace($patterns, $replacements, $string); ?>
【preg_replace_callback()】
preg_replace_callback()执行一个正则表达式搜索并且使用一个回调进行替换
mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] )
<?php // 将文本中的年份增加一年. $text = "April fools day is 04/01/2002"; $text.= "Last christmas was 12/24/2001"; // 回调函数 function next_year($matches){ // 通常: $matches[0]是完成的匹配 // $matches[1]是第一个捕获子组的匹配 // 以此类推 return $matches[1].($matches[2]+1);}>
【preg_filter()】
preg_filter() 执行一个正则表达式搜索和替换,等价于preg_replace()除了它仅仅返回(可能经过转化)与目标匹配的结果
mixed preg_filter ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
<?php $subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4'); $pattern = array('/d/', '/[a-z]/', '/[1a]/'); $replace = array('A:$0', 'B:$0', 'C:$0'); ?>
分割
【preg_split()】 preg_split()通过一个正则表达式分隔字符串 array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
如果指定limit,将限制分隔得到的子串最多只有limit个,返回的最后一个子串将包含所有剩余部分。limit值为-1,0或null时都代表"不限制";可以使用null跳过对flags的设置
flags可以是任何下面标记的组合(以位或运算 | 组合):PREG_SPLIT_NO_EMPTY——如果这个标记被设置,preg_split()将进返回分隔后的非空部分;PREG_SPLIT_DELIM_CAPTURE——如果这个标记设置了,用于分隔的模式中的括号表达式将被捕获并返回;PREG_SPLIT_OFFSET_CAPTURE——如果这个标记被设置,对于每一个出现的匹配返回时将会附加字符串偏移量。注意:这将会改变返回数组中的每一个元素,使其每个元素成为一个由第0个元素为分隔后的子串,第1个元素为该子串在subject中的偏移量组成的数组
<?php//使用逗号或空格(包含" ", , , , f)分隔短语$keywords = preg_split("/[s,]+/", "hypertext language, programming");/*Array ( [0] => hypertext [1] => language [2] => programming ) */print_r($keywords);?>
转义
【preg_quote()】
preg_quote()转义正则表达式字符
string preg_quote ( string $str [, string $delimiter = NULL ] )
正则表达式特殊字符有: . + * ? [ ^ ] $ ( ) { } = ! | : -
<?php$keywords = '$40 for a g3/400';$keywords = preg_quote($keywords, '/');echo $keywords; // 返回 $40 for a g3/400?>
以上就是前端学PHP之正则表达式函数的全部内容。
相关推荐:php中文网
The above is the detailed content of PHP regular expression function. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
