本文介绍了将字符串分解成没有空元素的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PHP的explode函数返回一个字符串数组,该字符串数组拆分为某些提供的子字符串.出现前导,尾随或连续定界符时,它将返回空字符串,如下所示:
PHP's explode function returns an array of strings split on some provided substring. It will return empty strings when there are leading, trailing, or consecutive delimiters, like this:
var_dump(explode('/', '1/2//3/'));
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(0) ""
[3]=>
string(1) "3"
[4]=>
string(0) ""
}
是否有一些不同的函数或选项,或者会返回除空字符串以外的所有内容的?
Is there some different function or option or anything that would return everything except the empty strings?
var_dump(different_explode('/', '1/2//3/'));
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
推荐答案
尝试 preg_split .
$ exploded = preg_split('@/@','1/2//3/',NULL,PREG_SPLIT_NO_EMPTY);
这篇关于将字符串分解成没有空元素的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!