PHP 如果下一个字符是 space 或大写字母字符,则按点分解字符串

PHP explode string by dot if the next character is a space or an uppercase alphabetic character

我有一段看起来像这样:

Lorem Ipsum is simply (not 1.2%) dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s

我想将其拆分为以点 . 结尾的短语中的文字,但前提是该点位于短语的末尾,而不是中间(如 1.2%)并且当它后面有一个 UPPERCASE 字符(也可能是一个空白的 space )。例如,如果我使用:

$arr = explode('.', $paragraph);

它会在每次出现 . 时拆分该段落。

有没有快速干净的方法来获得它?如果是,有人可以帮助我理解吗?

使用正则表达式匹配大写字符或 space 之前的点,并使用 preg_split() 根据正则表达式匹配拆分字符串。

$arr = preg_split("/\.\s?(?=[A-Z])/", $paragraph);

检查结果 demo

使用preg_split('/\.[\s|$]/', $input_line);

这将在点和 space 或新行上拆分。

https://www.phpliveregex.com/p/qiC
https://3v4l.org/FOaHu