Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explode a paragraph into sentences in PHP

I have been using

explode(".",$mystring)

to split a paragraph into sentences. However this doen't cover sentences that have been concluded with different punctuation such as ! ? : ;

Is there a way of using an array as a delimiter instead of a single character? Alternativly is there another neat way of splitting using various punctuation?

I tried

explode(("." || "?" || "!"),$mystring)

hopefully but it didn't work...

like image 748
Chris Headleand Avatar asked May 08 '12 07:05

Chris Headleand


4 Answers

You can use preg_split() combined with a PCRE lookahead condition to split the string after each occurance of ., ;, :, ?, !, .. while keeping the actual punctuation intact:

Code:

$subject = 'abc sdfs.    def ghi; this is [email protected]! asdasdasd? abc xyz';
// split on whitespace between sentences preceded by a punctuation mark
$result = preg_split('/(?<=[.?!;:])\s+/', $subject, -1, PREG_SPLIT_NO_EMPTY);
print_r($result);

Result:

Array
(
    [0] => abc sdfs.
    [1] => def ghi;
    [2] => this is [email protected]!
    [3] => asdasdasd?
    [4] => abc xyz
)

You can also add a blacklist for abbreviations (Mr., Mrs., Dr., ..) that should not be split into own sentences by inserting a negative lookbehind assertion:

$subject = 'abc sdfs.   Dr. Foo said he is not a sentence; asdasdasd? abc xyz';
// split on whitespace between sentences preceded by a punctuation mark
$result = preg_split('/(?<!Mr.|Mrs.|Dr.)(?<=[.?!;:])\s+/', $subject, -1, PREG_SPLIT_NO_EMPTY);
print_r($result);

Result:

Array
(
    [0] => abc sdfs.
    [1] => Dr. Foo said he is not a sentence;
    [2] => asdasdasd?
    [3] => abc xyz
)
like image 150
Kaii Avatar answered Oct 07 '22 13:10

Kaii


You can do:

preg_split('/\.|\?|!/',$mystring);

or (simpler):

preg_split('/[.?!]/',$mystring);
like image 24
codaddict Avatar answered Oct 07 '22 14:10

codaddict


Assuming that you actually want the punctuations marks with the end result, have you tried:

 $mystring = str_replace("?","?---",str_replace(".",".---",str_replace("!","!---",$mystring)));
 $tmp = explode("---",$mystring);

Which would leave your punctuation marks in tact.

like image 24
Dave Holitish Avatar answered Oct 07 '22 14:10

Dave Holitish


preg_split('/\s+|[.?!]/',$string);

A possible problem might be if there is an email address as it could split it onto a new line half way through.

like image 26
Darren Burgess Avatar answered Oct 07 '22 15:10

Darren Burgess