我正在使用以下正则表达式将字符串拆分为数组。一切顺利,但由于某种原因,它没有用 \.(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/