有没有一种方法可以使用对之前捕获组的反向引用作为命名捕获组的名称?如果不可能,那么这也是一个有效的答案。
以下内容:
$data = 'description: some description';
preg_match("/([^:]+): (.*)/", $data, $matches);
print_r($matches);
产出:
(
[0] => description: some description
[1] => description
[2] => some description
)
我尝试使用一个回溯引用来引用第一个捕获组作为命名捕获组(?<$1>.*)
,但告诉我这要么是不可能的,要么我只是没有正确地实现:
preg_match("/([^:]+): (?<$1>.*)/", $data, $matches);
产生:
警告:preg_match():编译失败:在偏移量12处的(?<后面有无法识别的字符
期望的结果应该是:
(
[0] => description: some description
[1] => description
[description] => some description
)
使用 preg_match
进行了简化。当使用 preg_match_all
时,我通常使用以下代码:
$matches = array_combine($matches[1], $matches[2]);
But thought I might be slicker than that.