preg_match_all中第四个参数PREG_PATTERN_ORDER,PREG_SET_OR
时间: 2023-08-03 13:03:11 浏览: 140
DER和PREG_OFFSET_CAPTURE有什么区别?
PREG_PATTERN_ORDER是默认参数,表示按照正则表达式匹配的顺序返回结果数组,例如:
```
$str = 'hello world';
$pattern = '/\w+/';
preg_match_all($pattern, $str, $matches);
print_r($matches);
```
输出结果为:
```
Array
(
[0] => Array
(
[0] => hello
[1] => world
)
)
```
PREG_SET_ORDER表示按照匹配到的结果分组返回结果数组,例如:
```
$str = 'hello world';
$pattern = '/(\w)(\w+)/';
preg_match_all($pattern, $str, $matches, PREG_SET_ORDER);
print_r($matches);
```
输出结果为:
```
Array
(
[0] => Array
(
[0] => hello
[1] => h
[2] => ello
)
[1] => Array
(
[0] => world
[1] => w
[2] => orld
)
)
```
PREG_OFFSET_CAPTURE表示同时返回匹配到的结果和它们在原字符串中的偏移量,例如:
```
$str = 'hello world';
$pattern = '/\w+/';
preg_match_all($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
```
输出结果为:
```
Array
(
[0] => Array
(
[0] => Array
(
[0] => hello
[1] => 0
)
[1] => Array
(
[0] => world
[1] => 6
)
)
)
```
注意:以上示例代码仅供参考,实际使用时需要根据具体的需求进行调整。
阅读全文