douqiong8412 2016-05-30 07:59
浏览 144
已采纳

PHP preg_split或preg_match句子但在Array中保留标点符号

I am looking to breakdown a paragraph into sentences and then into 'exploded' strings but need to keep the punctuations as elements of the array.

Example text:

$meta = 'I am looking to break this paragraph into chunks. 
         I have researched, tried and tested various combinations; however, I cannot
         seem to make it work. Would anyone help me figure this out? 
         I thank you in advance...'

The desired output would be:

Array ( [0] =>
           Array ( [0] => I [1] => am [2] => looking [3] => to [4] => break [5] => [6] => this [7] => paragraph [8] => into [9] => chunks [10] => . )
        [1] =>    
           Array ( [0] => I [2] => have [3] => researched [4] => , [5] => tried [......
          ......] [5] => figure [6] => this [7] => out [8] => ? )
        [3] =>
          Array ( [0] => I [1] => thank [2] => you [3] => in [4] => advance [5] => ... )
      )

I have tried using:

$s = preg_split('/\s*[!?.]\s*/u', $meta, -1, PREG_SPLIT_NO_EMPTY);

to separate out the sentences but whilst this works, the punctuation disappears.

I would really appreciate help with building this two level array with the punctuation

  • 写回答

1条回答 默认 最新

  • doulaozhang0238 2016-05-30 08:07
    关注

    You could do what you want using preg_match:

    $meta = 'I am looking to break this paragraph into chunks. 
             I have researched, tried and tested various combinations; however, I cannot
             seem to make it work. Would anyone help me figure this out? 
             I thank you in advance...';
    
    preg_match_all('/(\w+|[.;?,]+)/', $meta, $m);
    print_r($m);
    

    Explanation:

    /           : regex delimiter
      (         : begin group 1
        \w+     : 1 or more aphanumeric character <=> [a-zA-Z0-9_]
        |       : OR
        [.;?,]+ : 1 or more punctuation
      )         : end of group 1
    /           : regex delimiter
    

    This will match and store in group 1 evry word an every group of punctuiation character.

    If you want to be unicode compatible, you could use \p{L} for any letter and \p{P} for punctuation:

    /(\p{L}+|\p{P}+)/
    

    Output:

    Array
    (
        [0] => Array
            (
                [0] => I
                [1] => am
                [2] => looking
                [3] => to
                [4] => break
                [5] => this
                [6] => paragraph
                [7] => into
                [8] => chunks
                [9] => .
                [10] => I
                [11] => have
                [12] => researched
                [13] => ,
                [14] => tried
                [15] => and
                [16] => tested
                [17] => various
                [18] => combinations
                [19] => ;
                [20] => however
                [21] => ,
                [22] => I
                [23] => cannot
                [24] => seem
                [25] => to
                [26] => make
                [27] => it
                [28] => work
                [29] => .
                [30] => Would
                [31] => anyone
                [32] => help
                [33] => me
                [34] => figure
                [35] => this
                [36] => out
                [37] => ?
                [38] => I
                [39] => thank
                [40] => you
                [41] => in
                [42] => advance
                [43] => ...
            )
    
        [1] => Array
            (
                [0] => I
                [1] => am
                [2] => looking
                [3] => to
                [4] => break
                [5] => this
                [6] => paragraph
                [7] => into
                [8] => chunks
                [9] => .
                [10] => I
                [11] => have
                [12] => researched
                [13] => ,
                [14] => tried
                [15] => and
                [16] => tested
                [17] => various
                [18] => combinations
                [19] => ;
                [20] => however
                [21] => ,
                [22] => I
                [23] => cannot
                [24] => seem
                [25] => to
                [26] => make
                [27] => it
                [28] => work
                [29] => .
                [30] => Would
                [31] => anyone
                [32] => help
                [33] => me
                [34] => figure
                [35] => this
                [36] => out
                [37] => ?
                [38] => I
                [39] => thank
                [40] => you
                [41] => in
                [42] => advance
                [43] => ...
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。