-1

我有这句话:"Hello, how are you?"。我想用单词来拆分它。我可以使用该split()功能,但我想收到这个结果:

array => [1] 'Hello',
         [2] ', how',
         [3] 'are',
         [4] 'you?';

请帮助任何人,因为我不太擅长正则表达式。谢谢!

4

2 回答 2

2

尝试这个:

$result = preg_split('/\b(?=\p{P} )|\b /', 'Hello, how are you?');
于 2013-10-10T16:18:46.103 回答
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:]-] )
        |  
           [?.!] 
      )*
 )
于 2013-10-10T17:41:37.107 回答