Out of curiosity (oh, and suspecting mine would be the fastest 🙂), I did this test script:
$friends = ',,jeff,john,,,clark,,geoff,,,sean,,mark,iola,,,,ione,,,isabel,judith,,,,,jane,janet,,';
$start = microtime(true);
for ($i = 1; $i < 100000; $i++) {
$friendsArray = array(strtok($friends, ','));
while (($item = strtok(',')) !== false) {
$friendsArray[] = $item;
}
}
echo (microtime(true) - $start) . '<br />';
print_r($friendsArray);
$start = microtime(true);
for ($i = 1; $i < 100000; $i++) {
$friendsArray = explode(',', $friends);
$friendsArray = array_diff($friendsArray, array(""));
}
echo (microtime(true) - $start) . '<br />';
print_r($friendsArray);
$start = microtime(true);
for ($i = 1; $i < 100000; $i++) {
$friendsArray = explode(",", preg_replace('/,{2,}/', ',', $friends));
}
echo (microtime(true) - $start) . '<br />';
print_r($friendsArray);
with these typical results:
3.5671348571777
Array ( [0] => jeff [1] => john [2] => clark [3] => geoff [4] => sean [5] => mark [6] => iola [7] => ione [8] => isabel [9] => judith [10] => jane [11] => janet )
6.4273660182953
Array ( [2] => jeff [3] => john [6] => clark [8] => geoff [11] => sean [13] => mark [14] => iola [18] => ione [21] => isabel [22] => judith [27] => jane [28] => janet )
4.5079989433289
Array ( [0] => [1] => jeff [2] => john [3] => clark [4] => geoff [5] => sean [6] => mark [7] => iola [8] => ione [9] => isabel [10] => judith [11] => jane [12] => janet [13] => )
The times are pretty consistent run to run and with different orders.
As you can see, stolzyboy's results need reindexing, and NogDog's routine needs tweaking to remove empty elements at the front and back.
hth