我使用这个preg_split
来分割一个pharagraph,只要后面有空格或大写字母字符的点:
$p = "This is a dummy text. And it has a new line in it n it also has dots. Byt not";
$arr = preg_split('/.s*?(?=[A-Z])/', $p);
这也防止了段落中的最后一个点被拆分(这是我想要的(,但是,我现在也需要它来拆分新行(或行(。我无法用我的头脑去理解它…:(
试试这个:
$arr = preg_split('/.s*?(?=[A-Z])|(rn|n|r)/', $p);
您可以使用一个替换来进行拆分,该替换匹配一个点,后跟0+水平空白字符,然后是1+大写字符,或者|
一个unicode换行序列:
.(?=h*[A-Z])|R+
例如:
$p = "This is a dummy text. And it has a new line in it n it also has dots. Byt not.";
$arr = preg_split('/.(?=h*[A-Z])|R+/', $p);
查看Regex演示|php演示