我在使用preg_match构造多维数组时遇到了一些困难。
我正试着把一段话分解成句子。然后,对于段落中的每个部分/句子,我想将每个单词和标点符号分解为数组的另一个级别。
@托托昨天帮助我进行了预赛,在保持标点符号为元素的同时爆开了弦。
然而,我一直在努力构建我想要的数组。
请考虑这样的一段:
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的内容,但是现在,我更困惑于数组复制每个部分并将它们添加到数组中吗?
$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);发布于 2016-05-31 01:17:20
您就快到了,我刚刚添加了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] => .
)
)https://stackoverflow.com/questions/37535325
复制相似问题