您的位置:首页 > 编程语言 > PHP开发

js实现类似php的explode功能

2018-03-15 00:00 513 查看
调用方法和返回值和php一样

function explode(separators,inputstring,includeEmpties) {
inputstring = new String(inputstring);
separators = new String(separators);

if(separators == "undefined") {
separators = " :;";
}
fixedExplode = new Array(1);
currentElement = "";
count = 0;

for(x=0; x < inputstring.length; x++) {
str = inputstring.charAt(x);
if(separators.indexOf(str) != -1) {
if ( ( (includeEmpties <= 0) || (includeEmpties == false)) && (currentElement == "")) {

}else{
fixedExplode[count] = currentElement;
count++;
currentElement = "";
}
}else{
currentElement += str;
}
}

if (( ! (includeEmpties <= 0) && (includeEmpties != false)) || (currentElement != "")) {
fixedExplode[count] = currentElement;
}
return fixedExplode;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  explode js php