Php 如何按字母和句号拆分?

Php 如何按字母和句号拆分?,php,regex,preg-replace,Php,Regex,Preg Replace,我想按字母和句点规则拆分文本。所以我这样做: $text = 'One two. Three test. And yet another one'; $splitted_text = preg_split("/\w\./", $text); print_r($splitted_text); 然后我得到这个: Array ( [0] => One tw [1] => Three tes [2] => And yet another one ) 但我确实需要这样: Array

我想按字母和句点规则拆分文本。所以我这样做:

$text = 'One two. Three test. And yet another one';
$splitted_text = preg_split("/\w\./", $text);
print_r($splitted_text);
然后我得到这个:

Array ( [0] => One tw [1] => Three tes [2] => And yet another one )
但我确实需要这样:

Array ( [0] => One two [1] => Three test [2] => And yet another one )
如何解决这件事?

使用声明

$text = 'One two. Three test. And yet another one';
$splitted_text = explode(".", $text);
print_r($splitted_text);
更新

$splitted_text = explode(". ", $text);
使用。explode语句还检查空格

您可以使用任何类型的分隔符,也可以使用短语,而不仅仅是单个字符

$text = 'One two. Three test. And yet another one';
$splitted_text = explode(".", $text);
print_r($splitted_text);
更新

$splitted_text = explode(". ", $text);
使用。explode语句还检查空格


您可以使用任何类型的分隔符,也可以使用一个短语,而不仅仅是一个字符,使用regex在这里是一种过度使用,您可以轻松地使用explode。既然已经给出了基于爆炸的答案,我将给出一个基于正则表达式的答案:

$splitted_text = preg_split("/\.\s*/", $text);
使用的正则表达式:\。\s*

\-点是元字符。为了匹配文字匹配,我们将其转义。 \s*-零个或多个空白。 如果使用正则表达式:\


在创建的一些片段中,您将有一些前导空格。

在这里使用正则表达式是一种过分的做法,您可以轻松地使用explode。既然已经给出了基于爆炸的答案,我将给出一个基于正则表达式的答案:

$splitted_text = preg_split("/\.\s*/", $text);
使用的正则表达式:\。\s*

\-点是元字符。为了匹配文字匹配,我们将其转义。 \s*-零个或多个空白。 如果使用正则表达式:\


您将在创建的某些片段中使用一些前导空格。

其在字母和句点上的拆分。如果要测试以确保句点之前有一个字母,则需要使用肯定的lookbehind断言

$text = 'One two. Three test. And yet another one';
$splitted_text = preg_split("/(?<=\w)\./", $text);
print_r($splitted_text);

它在字母和句号上分开。如果要测试以确保句点之前有一个字母,则需要使用肯定的lookbehind断言

$text = 'One two. Three test. And yet another one';
$splitted_text = preg_split("/(?<=\w)\./", $text);
print_r($splitted_text);

可能要使用分隔符。在这种情况下,我们要摆脱空间,但是的。此。可能要使用分隔符。在这种情况下,我们要摆脱空间,但是的。这