PHP中的多个分隔符拆分字符串

Sha*_*imi 5 php

可以根据多个分隔符将字符串解析为数组吗?如代码中所述:

$str ="a,b c,d;e f";
//What i want is to convert this string into array
//using the delimiters space, comma, semicolon
Run Code Online (Sandbox Code Playgroud)

ale*_*lex 17

PHP

$str = "a,b c,d;e f";

$pieces = preg_split('/[, ;]/', $str);

var_dump($pieces);
Run Code Online (Sandbox Code Playgroud)

CodePad.

产量

array(6) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
}
Run Code Online (Sandbox Code Playgroud)

  • @KyleFarris我经常使用它来分割用户在逗号上输入的输入,而不是使用`explode()`,`array_map()`和`trim()`,我一直在使用`preg_split('/,\ s*/',$ csv)` (3认同)