我正在使用以下正则表达式将字符串拆分为数组。一切都很顺利,但由于某种原因,它不会与\.(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);
答案 0 :(得分:8)
你的空白是这里的问题。正则表达式将此考虑在内,因此将其更改为:
$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);
请注意第一个/
之后和最终/
之前的空格是如何删除的。
答案 1 :(得分:1)
由于你使用的是双引号,你必须双点转义点,所以\\.
而不是\.