Split String by Multiple Delimiters in PHP

2020-07-16 08:07发布

Can strings be parsed into an array based on multiple delimiters? as explained in the code:

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

标签: php
2条回答
祖国的老花朵
3楼-- · 2020-07-16 08:52

PHP

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

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

var_dump($pieces);

CodePad.

Output

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"
}
查看更多
登录 后发表回答