Explode a paragraph into sentences in PHP

喜你入骨 提交于 2019-11-27 09:16:22
codaddict

You can do:

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

or (simpler):

preg_split('/[.?!]/',$mystring);

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 an.email@addre.ss! 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 an.email@addre.ss!
    [3] => asdasdasd?
    [4] => abc xyz
)

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.

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.

Use preg_split and give it a regex like [\.|\?!] to split on

You can try preg_split

$sentences = preg_split("/[\.\?\!,;]+/", $mystring);

Please note this will remove the punctuations. If you would like to strip out leading or trailing whitespace as well

$sentences = preg_split("/[\.\?\!,;]+\s+?/", $mystring);
Michael Manoochehri
$mylist = preg_split("/[\.|\?!:;]/", $mystring);
phpzag

You can't have multiple delimiters for explode. That's what preg_split(); is for. But even then, it explodes at the delimiter, so you will get sentences returned without the punctuation marks. You can take preg_split a step farther and flag it to return them in their own elements with PREG_SPLIT_DELIM_CAPTURE and then run some loop to implode sentence and following punctation mark in the returned array, or just use preg_match_all();:

preg_match_all('~.*?[?.!]~s', $string, $sentences);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!