PHP preg_split 正则表达式不与点分割

标签 php regex preg-split

我正在使用以下正则表达式将字符串拆分为数组。一切顺利,但由于某种原因,它没有用 \.(space) 进行拆分。我需要如何更改它才能使其正常工作?

  $sentences  = preg_split(" / (\. |, and|, or|, but|, nor|, so|, for|, yet|after|although|as|as if|as long as|because|before|even if|even though|if|once|provided|since|so that|that|though|till|unless|until|what|when|whenever|wherever|whether|while) /",$sentences); 

最佳答案

您的空格是这里的问题。正则表达式考虑到了这一点,因此将其更改为:

$sentences = preg_split("/(\. |, and|, or|, but|, nor|, so|, for|, yet|after|although|as|as if|as long as|because|before|even if|even though|if|once|provided|since|so that|that|though|till|unless|until|what|when|whenever|wherever|whether|while)/",$sentences); 

请注意第一个 / 之后和最后一个 / 之前的空格是如何被删除的。

关于PHP preg_split 正则表达式不与点分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15657303/

相关文章:

php - CakePHP 1.3 : $this->render() vs $this->element()

php - 从 mysql 中删除值等于某物的重复行

.net - 正则表达式由逗号分隔而不是在括号内(.NET)

C# 正则表达式匹配查找几乎相同的字符串

php - 拆分字符串中的第一个空格和最后一个空格并限制输出的大小 3

PHP:类或全局的数据库连接

php - 使用 SimpleImage.php 类调整 PNG 透明度大小

javascript - 使用 !用于正则表达式编号验证

php:拆分字符串直到第一次出现数字

php - 如何用任何整数分解字符串?正则表达式?