字符串能否根据多个分隔符解析成数组?如下代码所示:
$str ="a,b c,d;e f";
//What i want is to convert this string into array
//using the delimiters space, comma, semicolon
字符串能否根据多个分隔符解析成数组?如下代码所示:
$str ="a,b c,d;e f";
//What i want is to convert this string into array
//using the delimiters space, comma, semicolon
$str = "a,b c,d;e f";
$pieces = preg_split('/[, ;]/', $str);
var_dump($pieces);
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"
}
strtok --> http://php.net/manual/zh/function.strtok.php
preg_split('/,\s*/', $csv)
,而不是使用explode()
、array_map()
和trim()
。 - alex