0

我花了最后一个小时寻找答复,但我还没有找到任何答复,所以我在这里问...

我需要一种方法(当然是正则表达式,但像explode这样的其他一切都很好)将如下的句子分成几个部分,在同一个数组中:

这是第一部分,这是第二部分;这是第三部分!这是第四部分?一次又一次——直到句子结束。

我想要一个包含以下条目的数组(请不要在标点符号之后或之前有空格):

  • [0] => "这是第一部分"
  • [1] => “这是第二部分”
  • [2] => "这是第三部分"
  • [3] => "这是第四部分"
  • [4] => “再次”
  • [5] => “又一次”
  • [6] => "直到句子结束"

编辑:对不起,下面的例子是英文的,但它应该能够处理各种各样的脚本(基本上都是 Unicode)。

非常感谢!

4

3 回答 3

1

一个人preg_split就可以完成这项工作:

$s = 'This is the first part, this is the second part; this is the third part! this is the fourth part? again - and again - until the sentence is over.';
print_r(preg_split('/\s*[,:;!?.-]\s*/u', $s, -1, PREG_SPLIT_NO_EMPTY));

输出:

Array
(
    [0] => This is the first part
    [1] => this is the second part
    [2] => this is the third part
    [3] => this is the fourth part
    [4] => again
    [5] => and again
    [6] => until the sentence is over
)
于 2013-10-23T10:00:39.463 回答
1

我在这里找到了解决方案

这是我使用多个分隔符来扩展输出的方法。

<?php

//$delimiters has to be array
//$string has to be array

function multiexplode ($delimiters,$string) {

    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);

print_r($exploded);

//And output will be like this:
// Array
// (
//    [0] => here is a sample
//    [1] =>  this text
//    [2] =>  and this will be exploded
//    [3] =>  this also
//    [4] =>  this one too
//    [5] => )
// )

?>
于 2013-10-23T09:10:24.197 回答
0

尝试使用这个

$parts = preg_split("/[^A-Z\s]+/i", $string);
var_dump($parts);
于 2013-10-23T09:09:29.103 回答