正則表達式的功能只要就是搜索和替換
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)