范例来自于 PHP手册
function multiexplode ($delimiters,$string) {
//将数组中的所有参数,都替换成数组的第一个
$ready = str_replace($delimiters, $delimiters[0], $string);
//再用数组的第一个参数把字符串分割成一个数组
$launch = explode($delimiters[0], $ready);
return $launch;
}
$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);
print_r($exploded);
上面的代码等同于:
$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = array(",",".","|",":");
//使用正则表达式,来分割字符串
$arr = preg_split("/[,\.|:]/",$text);
print_r($arr);
‘a=>1, b=>23 , $a, c=> 45% , true,d => ab c ,6’ 转换成数组
function string2KeyedArray($string, $delimiter = ',', $kv = '=>') {
if ($a = explode($delimiter, $string)) { // create parts
foreach ($a as $s) { // each part
if ($s) {
if ($pos = strpos($s, $kv)) { // key/value delimiter
$ka[trim(substr($s, 0, $pos))] = trim(substr($s, $pos + strlen($kv)));
} else { // key delimiter not found
$ka[] = trim($s);
}
}
}
return $ka;
}
} // string2KeyedArray
$string = 'a=>1, b=>23 , $a, c=> 45% , true,d => ab c ,6';
print_r(string2KeyedArray($string));