0

I have spent the last hour looking for replies but I haven't found any yet, so here I ask...

I need a way (certainly regex, but everything else like explode is fine) to cut a sentence like the following into parts, in the same array:

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.

I want an array with the following entries (without the spaces following or preceding or not the punctuation marks, please):

  • [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"

EDIT: Sorry, the following example is in English but it should be able to handle a whole variety of scripts (all of Unicode, basically).

Thanks a lot!

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 に答える