首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用preg-match在预测中推入多维数组

使用preg-match在预测中推入多维数组
EN

Stack Overflow用户
提问于 2016-05-31 00:58:46
回答 1查看 83关注 0票数 2

我在使用preg_match构造多维数组时遇到了一些困难。

我正试着把一段话分解成句子。然后,对于段落中的每个部分/句子,我想将每个单词和标点符号分解为数组的另一个级别。

@托托昨天帮助我进行了预赛,在保持标点符号为元素的同时爆开了弦。

然而,我一直在努力构建我想要的数组。

请考虑这样的一段:

代码语言:javascript
复制
First section. This section, and this. How about this section? And a section; split in two.

期望输出

作为回报,结果如下:

代码语言:javascript
复制
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的内容,但是现在,我更困惑于数组复制每个部分并将它们添加到数组中吗?

代码语言:javascript
复制
$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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-31 01:17:20

您就快到了,我刚刚添加了PREG_SPLIT_DELIM_CAPTURE并更改了preg_split的正则表达式。所以你可以用这样的方式:

代码语言:javascript
复制
$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);

结果:

代码语言:javascript
复制
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] => .
        )

)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37535325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档