如果在某些情况下存在无法分隔的点分隔符,如何使用点分隔符拆分文本

时间:2017-05-25 15:47:24

标签: php arrays regex

示例文字:

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


我想得到这样的输出:

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

如果我使用explode() '.'作为第一个参数,则itlogic.comMrs.leafa会分开。

3 个答案:

答案 0 :(得分:3)

我认为preg_split是一个很好的工具,因为点之后可能有或没有空格,对吧?

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

说明:

  • \.匹配一段时间

  • (?=\s|$)然后断言空白字符或行尾

请参阅此处:点击preg_split,http://www.phpliveregex.com/p/kdz

答案 1 :(得分:2)

更新#2

正则表达式:

(?(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代码:

$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));

输出:

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

答案 2 :(得分:1)

尝试一次

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