Example #4 Using named subpattern
<?php
$str
=
'foobar: 2008'
;
preg_match
(
'/(?<name>/w+): (?<digit>/d+)/'
,
$str
,
$matches
);
print_r
(
$matches
);
?>
The above example will output:
Array
(
[0] => foobar: 2008
[name] => foobar
[1] => foobar
[digit] => 2008
[2] => 2008
以上例子在php5.16是有问题的,将输出一个空的数组,意思是匹配不到。如要这样才行:
<?php
$str
=
'foobar: 2008'
;
preg_match
(
'/(?<name>/w+): (?P<digit>/d+)/'
,
$str
,
$matches
);
print_r
(
$matches
);
?>
差异就在于(?<与(?P< 。