使用分隔符条缩写分割字符串

时间:2014-08-20 10:23:17

标签: php string split explode

假设我想基于分隔符拆分字符串。所以我很简单地做explode('. ', $myString)。但是,如果存在U.K.U.S.之类的缩写,则会删除。

那么,我怎么能使用explode保持所有缩写完整。

缩写的格式为:X.Y.Z

句子由.分隔 例如,The U.S. is a country. It's in N.America.应该导致:

[0] = The U.S. is a country.
[1] = It's in N.America.

1 个答案:

答案 0 :(得分:0)

基于Split a text into sentences

preg_split('/(?<=[.?!])\s+(?=[A-Z])/', "The U.S. is a country. It's in N.America.")

输出:

array(2) {
  [0]=>
  string(22) "The U.S. is a country."
  [1]=>
  string(18) "It's in N.America."
}