Skip to content

Instantly share code, notes, and snippets.

@pedropapa
Last active August 29, 2015 14:05
Show Gist options
  • Save pedropapa/331a22e1c0772c4ff396 to your computer and use it in GitHub Desktop.
Save pedropapa/331a22e1c0772c4ff396 to your computer and use it in GitHub Desktop.
Função explode
<?php
/*
"recriando a roda", função explode()
By: Papadópolis (@papadopolis)
*/
function _explode($op,$str,$lim = null) {
$output = array();
preg_match_all("/(.*?$op|.*?$)/i",$str,$vals);
unset($vals[0],$vals[1][count($vals[1])-1]);
foreach($vals as $ind => $val)
foreach($val as $exe)
$output[] = str_replace($op,"",$exe);
if($lim !== null) {
if($lim>=0) {
$alg = array();
$tmp = $output;
$co = 0;
for(;;) {
$alg[] = $output[$co];
unset($tmp[$co]);
if($co == $lim-1) {
foreach($tmp as $val)
$alg[$co] .= sprintf("%s%s",$op,$val);
break;
}
++$co;
}
$output = $alg;
} else {
$f = count($output)-1;
$l = $f-substr($lim,1);
for(;;) {
unset($output[$f]);
--$f;
if($f == $l) break;
}
}
}
return $output;
}
$str = "a,e,i,o,u,1,2,3,4,5";
$str = _explode(",",$str,3);
print_r(array_values($str));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment