【问题标题】:Split string using delimiter strips abbreviations使用分隔符条缩写拆分字符串
【发布时间】:2014-10-13 16:04:36
【问题描述】:

假设,我想根据分隔符分割一个字符串。所以我会简单地做一个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.

【问题讨论】:

  • 检查:preg_split("/^.\./", "U.K.");
  • @Zentoaku:目前,我只使用这个。如果一个人可能不知道有多少或什么样的缩写词怎么办?任何通用的解决方案?问题已编辑。

标签: php string split explode


【解决方案1】:

基于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."
}

【讨论】:

  • 太棒了!谢谢。不知道为什么这个问题被某人否决了。
猜你喜欢
  • 1970-01-01
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
相关资源
最近更新 更多