This article provides a detailed analysis of PHP's implementation of the explicit () function. For more information, see
The code is as follows:
/**
* Explain how PHP implements the explicit () function
*/
$ Str = ', 1, 2, 4, 321321, aaa, bbb ';
Function myExplode ($ seg, $ str, $ limit = 0 ){
$ _ Ret = array (0 => '');
$ Len = strlen ($ str );
$ Slen = strlen ($ seg );
$ _ Limit = 0;
For ($ I = 0; $ I <$ len; $ I ++ ){
If (substr ($ str, $ I, $ slen) ==$ seg ){
$ _ Limit ++;
$ I + = $ slen-1;
Continue;
} Else {
$ _ Ret [$ _ limit]. = $ str [$ I];
}
}
If ($ limit <0)
$ _ Ret = array_slice ($ _ ret, 0, $ limit );
Else {
$ _ Ret = $ limit> = count ($ _ ret )? $ _ Ret: array_merge (array_slice ($ _ ret, 0, $ limit-1), array (implode ($ seg, array_slice ($ _ ret, $ limit-1 ))));
}
Return $ _ ret;
}
Var_dump (myExplode ("4", $ str ));
Echo"
";
Var_dump (explode ("4", $ str ));
Echo"
";
?>
The limit value can be negative .. Delimiter is a string