Rachmad
Rachmad

Reputation: 137

How can I split text with dots delimiter when in some case there are dot delimiter that can't be separated

Example text:

There is an unique news in itlogic.com. I was read it when Mrs.leafa is cooking.


I want to get output like this:

Array (
    [0] There is an unique news in itlogic.com.
    [1] I was read it when Mrs.leafa is cooking.
)

If I use explode() with '.' as the first parameter, itlogic.com and Mrs.leafa are separated.

Upvotes: 3

Views: 2001

Answers (3)

revo
revo

Reputation: 48751

Update #2

Regex:

(?(DEFINE)          # Construct a definition structure
  (?<punc>[!?.]+)     # Define `punc` group consisting of `.`, `?` and `!`
)                   # End of definition
\b                  # Match a word boundary position
(?>                 # Open a grouping (non-capturing) (a)
  [a-z0-9]            # Match a digit or a lower case letter
  \w*                 # And any number of word characters
  |                   # Or
  [A-Z]               # Match an upper case letter
  \w{3,}              # And word characters more than 3
  (?=                 # Followed by
    (?&punc)          # Any number of `.`, `?` and `!` characters
  )                   # End of positive lookahead
)                   # End of grouping (a)
(?&punc)            # Match any number of `.`, `?` and `!` characters
\K\B\s*             # Reset match, assert a NWB position + any number of whitespaces

Live demo

PHP code:

$str = 'There is an unique news in itlogic.com. I was read it when Mrs. leafa is cooking.';
print_r(preg_split($RE, $str, -1, PREG_SPLIT_NO_EMPTY));

Outputs:

Array
(
    [0] => There is an unique news in itlogic.com.
    [1] => I was read it when Mrs. leafa is cooking.
)

Upvotes: 2

Andreas
Andreas

Reputation: 23968

I think preg_split is a good tool for this as there may or may not be a space after the dot, right?

$array = preg_split("/\.(?=\s|$)/m", $Text);

Explanation:

  • \. Match a period

  • (?=\s|$) Then assert a whitespace character or end of line afterwards

See here: Click on preg_split, http://www.phpliveregex.com/p/kdz

Upvotes: 3

Exprator
Exprator

Reputation: 27523

try this once

$s= explode('. ',$your_sentence);

Upvotes: 2

Related Questions