PHP preg_split 为新行添加拆分
PHP preg_split add split for new line
我正在使用这个 preg_split
来拆分一个字符,只要有点后有 space 或大写字母字符:
$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])|(\r\n|\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);
我正在使用这个 preg_split
来拆分一个字符,只要有点后有 space 或大写字母字符:
$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])|(\r\n|\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);