私はこの文を持っています: "Hello, how are you?"
. 言葉で分けたい。関数を使用できますが、次のsplit()
結果を受け取りたいです。
array => [1] 'Hello',
[2] ', how',
[3] 'are',
[4] 'you?';
正規表現が苦手なので、どなたか教えてください。ありがとうございました!
これにより、 preg_split よりも柔軟性が向上します。
# $string = "Hello, how are you?";
#
# preg_match_all
# (
# '/\s*([^\pL\pN]*[\pL\pN](?:[\pL\pN_-]|\pP(?=[\pL\pN\pP_-])|[?.!])*)/',
# $string,
# $matches,
# PREG_PATTERN_ORDER
# );
# print_r( $matches[1] );
# ------------------------------------
# Result:
# Array
# (
# [0] => Hello
# [1] => , how
# [2] => are
# [3] => you?
# )
# Unicode
# \s*([^\pL\pN]*[\pL\pN](?:[\pL\pN_-]|\pP(?=[\pL\pN\pP_-])|[?.!])*)
\s* # Strip whitespace
(
[^\pL\pN]* [\pL\pN] # Not letters/numbers, followed by letter/number
(?:
[\pL\pN_-] # Letter/number or '-'
|
\pP # Or, punctuation if followed by punctuation/letter/number or '-'
(?= [\pL\pN\pP_-] )
|
[?.!] # Or, (Add) Special word ending punctuation
)*
)
# ASCII
# \s*([\W_]*[^\W_](?:\w|[[:punct:]_-](?=[\w[:punct:]-])|[?.!])*)
\s*
(
[\W_]* [^\W_]
(?:
\w
|
[[:punct:]_-]
(?= [\w[:punct:]-] )
|
[?.!]
)*
)