PHP正则表达式
2016-06-03 21:30
483 查看
PHP正则表达式
0x00
在这里对PHP中Perl风格的正则表达式函数做一个分类总结.在这里我们做如下约定:
$pattern*=正则表达式
$subject=匹配的目标数据
0x01
preg_match()函数原型:
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
该函数的返回值是0或者1,如果提供了参数matches那么它将会被填充为匹配到的文本。该函数是最多匹配一次。
preg_match_all()
函数原型:
int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
该函数的返回值是匹配的返回次数,并且它会匹配所有的结果。
注意这两个函数的array∗都是引用传递,直接修改∗matches的值。
0x02
preg_replace()该函数的函数原型:
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
返回替换的字符串
preg_filter
该函数的原型:
mixed preg_filter ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
也是返回替换后的字符串。
两者的区别在于,preg_replace在替换数组的情况无论有没有匹配都会保留数组中的元素,而preg_filter只会保留数组中被替换了的元素。
0x03
preg_grep它是被大大简化的preg_filter,它只匹配,不替换。它把数组中不能被匹配的都过滤掉。
0x04
preg_split函数原型:
array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
该函数会把匹配到的地方作为分割点将字符串分割后存入返回数组中。
0x05
preg_quote该函数可以把正则表达式中的特殊字符(. \ + ? [ ^ ] $ ( ) { } = ! < > | : -*)进行转译。
0x06正则表达式修正模式
未规定修正模式的时候自动是贪婪模式。$pattern = '/\d/U'这样就是懒惰模式。
那些常见的修正模式:
U-懒惰匹配
i-忽略英文字母大小写
x-忽略空白
s-让元字符’.’匹配换行符在内的所有字符
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- rem : web app适配的秘密武器
- jquery高级应用之Deferred对象
- php7 读取php.ini[4]
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- 简单好用的PHP分页类