Просто измените \w
на [^\s.]
(не пробел или точку) и удалите слова boudaries.Другое изменение заключается в добавлении чередования «начало строки ИЛИ пробел» в начале регулярного выражения:
$text = "The quick brown fox's feet jumped over the lazy dog. The rain falls head-first in the plain.";
preg_match_all("/(?=((?<=^|\s)[^\s.]+(?:\s+[^\s.]+|$)(?:\s+[^\s.]+|$)))/",$text,$matches);
print_r($matches[1]);
Вывод:
Array
(
[0] => The quick brown
[1] => quick brown fox's
[2] => brown fox's feet
[3] => fox's feet jumped
[4] => feet jumped over
[5] => jumped over the
[6] => over the lazy
[7] => the lazy dog
[8] => The rain falls
[9] => rain falls head-first
[10] => falls head-first in
[11] => head-first in the
[12] => in the plain
)
Объяснение регулярного выражения:
(?= # lookahead
( # start group 1
(?<=^|\s) # lookbehind, make sure we have beginning of line or space before
[^\s.]+ # 1 or more non space, non dot
(?: # non capture group
\s+ # 1 or more spaces
[^\s.]+ # 1 or more non space, non dot
| # OR
$ # end of line
) # end group
(?: # non capture group
\s+ # 1 or more spaces
[^\s.]+ # 1 or more non space, non dot
| # OR
$ # end of line
) # end group
) # end group 1
) # end lookahead
Редактировать в соответствии с комментарием.
$text = "The quick brown fox's feet jumped over the lazy dog. The rain falls head-first in the plain. 'This is a quote,' I say, and that's that.";
preg_match_all("/(?=((?<=^|\s|')(?:(?<=[a-zA-Z])'(?=[a-zA-Z])|[^\s.,'])+(?:\s+(?:(?<=[a-zA-Z])'(?=[a-zA-Z])|[^\s.,'])+|$){2}))/",$text,$matches);
print_r($matches[1]);
Вывод:
Array
(
[0] => The quick brown
[1] => quick brown fox's
[2] => brown fox's feet
[3] => fox's feet jumped
[4] => s feet jumped
[5] => feet jumped over
[6] => jumped over the
[7] => over the lazy
[8] => the lazy dog
[9] => The rain falls
[10] => rain falls head-first
[11] => falls head-first in
[12] => head-first in the
[13] => in the plain
[14] => This is a
[15] => is a quote
[16] => and that's that
)
Regex объяснение:
(?= # lookahead
( # start group 1
(?<=^|\s|') # lookbehind, make sure we have beginning of line or space or quote before
(?: # start non capture group
(?<=[a-zA-Z]) # lookbehind, make sure we have a letter before
' # a single quote
(?=[a-zA-Z]) # lookahead, make sure we have a letter after
| # OR
[^\s.,'] # not a space or dot or comma or single quote
)+ # group may appear 1 or more times
(?: # non capture group
\s+ # 1 or more spaces
(?: # non capture group
(?<=[a-zA-Z]) # lookbehind, make sure we have a letter before
' # a single quote
(?=[a-zA-Z]) # lookahead, make sure we have a letter after
| # OR
[^\s.,'] # not a space or dot or comma or single quote
)+ # group may appear 1 or more times
| # OR
$ # end of line
){2} # end group, must appear twice
) # end group 1
) # end lookahead