我有这句话:"Hello, how are you?"
。我想用文字分开。我可以使用split()
函数,但我想收到这个结果:
array => [1] 'Hello',
[2] ', how',
[3] 'are',
[4] 'you?';
请帮助任何人,因为我对正则表达式不是很好。谢谢!
答案 0 :(得分:2)
试试这个:
$result = preg_split('/\b(?=\p{P} )|\b /', 'Hello, how are you?');
答案 1 :(得分:0)
这将提供比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:]-] )
|
[?.!]
)*
)