[英]pushing to a multi-dimensional array in foreach using preg-match
我在使用 preg_match 构建多维数组时遇到了一些困难。
我试图将一个段落分解成句子。 然后对于段落的每个部分/句子,我想将每个单词和标点符号分解为数组的另一个级别。
@Toto 昨天帮助我使用预匹配来分解字符串,同时保留标点符号作为元素。
但是,我一直在努力构建我想要的数组。
考虑这样一个段落:
First section. This section, and this. How about this section? And a section; split in two.
作为回报,结果如下所示:
Array ( [0] =>
Array ( [0] => First [1] => section [2] => . )
Array ( [1] =>
Array ( [0] => This [1] => section [2] => , [3] => and [4] => this [2] => . )
Array ( [2] =>
Array ( [0] => How [1] => about [2] => this [3] => section [4] => ? )
Array ( [3] =>
Array ( [0] => And [1] => a [2] => section [3] => ; [4] => split
[5] => in [6] => two [7] => . )
)))
这是行不通的。 我不太确定一旦我构建了第二维,我将如何删除 $s 的内容,但现在我对复制每个部分并将它们添加到数组 [0] 的数组感到更加困惑?
$m = ' First section. This section, and this. How about this section? And a section; split in two.'
$s = preg_split('/\s*[!?.]\s*/u', $m, -1, PREG_SPLIT_NO_EMPTY);
foreach ($s as $x => $var) {
preg_match_all('/(\w+|[.;?!,:]+)/', $var, $a);
array_push($s, $a);
}
print_r($s);
你快接近了,我刚刚添加了PREG_SPLIT_DELIM_CAPTURE
并更改了preg_split
的正则表达式。 所以你可以这样使用:
$str = 'First section. This section, and this. How about this section? And a section; split in two.';
$matchDelim = preg_split("/([^.?!]+[.?!]+)/", $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$finalArr = [];
foreach ($matchDelim as $match) {
preg_match_all('/(\w+|[.;?!,:])/', $match, $matches);
$finalArr[] = $matches[0];
}
print_r($finalArr);
结果:
Array
(
[0] => Array
(
[0] => First
[1] => section
[2] => .
)
[1] => Array
(
[0] => This
[1] => section
[2] => ,
[3] => and
[4] => this
[5] => .
)
[2] => Array
(
[0] => How
[1] => about
[2] => this
[3] => section
[4] => ?
)
[3] => Array
(
[0] => And
[1] => a
[2] => section
[3] => ;
[4] => split
[5] => in
[6] => two
[7] => .
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.