使用preg_match
with subpattern 总是返回具有相同数据的双键数组,一个带有子模式名称,另一个带有数字标记。因为我要匹配数十万行,每行只有几 KB,恐怕数字数组会占用额外的内存。是否有任何适当的方法来禁用数字标签数组返回?
例子:
<?php
header('Content-Type: text/plain');
$data = <<<START
I go to school.
He goes to funeral.
START;
preg_match_all('@^(?<who>.*?) go(es)* to (?<place>.*?)$@m', $data, $matches);
print_r($matches);
?>
输出:
Array
(
[0] => Array
(
[0] => I go to school.
[1] => He goes to funeral.
)
[who] => Array
(
[0] => I
[1] => He
)
[1] => Array
(
[0] => I
[1] => He
)
[2] => Array
(
[0] =>
[1] => es
)
[place] => Array
(
[0] => school.
[1] => funeral.
)
[3] => Array
(
[0] => school.
[1] => funeral.
)
)