Something You Can Re-Use for Other Scenarios: (always better IMO).
While probably not the most elegant solution, this does follow the general concept syntax of other PHP core functions...
In any case... This uses recursion. It is flexible in that it allows you to specify the size of the chunk (in case you'd like to do that down the road or for a different project). I did this as more of a personal challenge to see what I could come up with.
<?php
function chunk_explode($glue=' ',$pieces='',$size=2,$final=array()) {
if(!is_string($pieces) && !is_array($pieces))
return false;
if(is_string($pieces))
$pieces = explode($glue,$pieces);
$num_pieces = sizeof($pieces);
if($num_pieces <= 0)
return $final;
if($num_pieces >= $size) {
$arr_chunk = array_chunk($pieces, $size);
array_push($final,implode($glue,$chunk[0]));
for($i=0;$i<=$size;$i++) { array_shift($pieces); }
return chunk_explode($glue,$pieces,$size,$final);
}
array_push($final,implode($glue,$pieces));
return $final;
}
$string = "This is my test case for an example.";
chunk_explode(' ',$string,3);
If this chunk_explode
function sucks, let me know so I can learn from my mistakes.