explode扩展-多符号分割字符串

时间:2023-01-03 11:19:58

PHP实现多符号分割字符串

function multiexplode($delimiter, $string){
$arr = explode(",", $string);
foreach ($delimiter as $v) {
if ($v != ","){
foreach ($arr as $kk=>$vv) {
$tmpArr[$kk] = explode($v, $vv);
}
$count = count($tmpArr);
$tmp = array();
for ($i=0; $i<$count; $i++) {
$tmp = array_merge($tmp, $tmpArr[$i]);
}
$arr = $tmp;
}
}
// 删除空白元素
foreach ($arr as $k=>$v) {
if ($v == "") {
unset($arr[$k]);
}
}
return $arr;
}

示例

$str = "aa@bb#cc_dd";
$arr = multiexplode(array("@", "#", "_"), $str);
echo "<pre>";
print_r($arr);

输出结果

Array
(
[0] => aa
[1] => bb
[2] => cc
[3] => dd
)

欢迎加入PHP技术交流群(478642311)