正则表达式的功能只要就是搜索和替换
php 的正则表达式的函数有这些:
# 搜索
## preg_match
```php
>>> $pattern = '/^a\w+f$/';
=> "/^a\w+f$/"
>>> $subject = 'adfgf';
=> "adfgf"
>>> preg_match($pattern, $subject,$matches);
=> 1
>>> $matches
=> [
"adfgf",
]
>>> $subject = 'adfd';
=> "adfd"
>>> preg_match($pattern, $subject,$matches);
=> 0
>>> $matches
=> []
```
## preg_match_all
一个全局正则表达式匹配
```php
>>> preg_match_all("/a\d+/", 'a4,a64', $matches);
=> 2
>>> $matches
=> [
[
"a4",
"a64",
],
]
>>> preg_match("/a\d+/", 'a4,a64', $matches);
=> 1
>>> $matches
=> [
"a4",
]
```
## preg_split
通过一个正则表达式分隔字符串
```php
>>> preg_split('/\d+/', 'l12d45f55addd')
=> [
"l",
"d",
"f",
"addd",
]
>>> preg_split('|[a-z]+|', 'l12d45f55addd')
=> [
"",
"12",
"45",
"55",
"",
]
```
## preg_grep
返回匹配的数组
```php
>>> preg_grep("/^(\d+)?\.\d+$/", ['12.3','56.2','45','ll'])
=> [
"12.3",
"56.2",
]
```
功能上和array_filter很像
# 替换
## preg_replace/preg_filter
这两个函数功能基本一样, 区别在底部
```php
>>> preg_replace('/sb/', '*', 'u are a sb')
=> "u are a *" // 这不就是关键字屏蔽么
>>> preg_filter('/sb/', '*', 'u are a sb')
=> "u are a *"
>>> $content = "Name: {Name}\nEmail: {Email}\nAddress: {Address}\n";
=> """
Name: {Name}\n
Email: {Email}\n
Address: {Address}\n
"""
>>> $patterns = ["/{Name}/", "/{Email}/", "/{Address}/"];
=> [
"/{Name}/",
"/{Email}/",
"/{Address}/",
]
>>> $replace = ["Jaime", "xsu@viewtool.com", "Chongqing China"];
=> [
"Jaime",
"xsu@viewtool.com",
"Chongqing China",
]
>>> echo preg_replace($patterns, $replace, $content);
Name: Jaime
Email: xsu@viewtool.com
Address: Chongqing China
=> null
>>> echo preg_filter($patterns, $replace, $content);
Name: Jaime
Email: xsu@viewtool.com
Address: Chongqing China
=> null
// 修改内容为数组
>>> $content = ['{Name}', '{Email}', 'Address', '{Address}' ]
=> [
"{Name}",
"{Email}",
"Address",
"{Address}",
]
>>> print_r(preg_replace($patterns, $replace, $content))
Array
(
[0] => Jaime
[1] => xsu@viewtool.com
[2] => Address
[3] => Chongqing China
)
=> true
>>> print_r(preg_filter($patterns, $replace, $content))
Array
(
[0] => Jaime
[1] => xsu@viewtool.com
[3] => Chongqing China
)
=> true
```
## preg_replace_callback
```php
<?php
$content = "end 1992-01-27\n";
$content.= "start 2007-08-30\n";
$pattern = '/\d{4}(-\d{2}){2}/';
// 回调函数, 把时间换成时间戳
function time_to_ts($matches)
{
var_dump($matches);
return strtotime($matches[0]);
}
echo preg_replace_callback($pattern, "time_to_ts", $content);
```
结果
```shell
$ php index.php
array(2) {
[0] =>
string(10) "1992-01-27"
[1] =>
string(3) "-27"
}
array(2) {
[0] =>
string(10) "2007-08-30"
[1] =>
string(3) "-30"
}
end 696441600
start 1188403200
```
## preg_replace_callback_array
这个没用过, 用的时候加上
# 辅助函数
## preg_quote
正则表达式特殊字符(*. \ + \* ? [ ^ ] $ ( ) { } = ! < > | : -)加上转义的斜线
```php
>>> preg_quote('. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -')
=> "\. \\ \+ \* \? \[ \^ \] \$ \( \) \{ \} \= \! \< \> \| \: \-"
```
## preg_last_error
获取最后一次正则表达式的错误
用法远不止这些, 更多的请看文档, 我这里只是写了我用到的
* [正则表达式语法](http://php.net/manual/zh/reference.pcre.pattern.syntax.php)
* [正则表达式文档](http://php.net/manual/zh/ref.pcre.php)