0

Suppose, I want to split a string based on delimiter. So I'd simple do a explode('. ', $myString). But, this strips out if there are any abbreviations present like U.K. or U.S..

So, how could I use explode keeping all abbreviations intact.

Abbreviations will be of the form: X.Y.Z

While sentences would be separated by . For example, The U.S. is a country. It's in N.America. should result in:

[0] = The U.S. is a country.
[1] = It's in N.America.
xan
  • 4,640
  • 13
  • 50
  • 83
  • Check: preg_split("/^.\. /", "U.K. "); – Zentoaku Aug 20 '14 at 10:32
  • @Zentoaku: Currently, I was using this only. What if one may not know how many or what kind of abbreviations are there? Any generalized solution? Question edited. – xan Aug 20 '14 at 10:35

1 Answers1

0

Based on Split a text into sentences:

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

Output:

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