Forum Moderators: coopster

Message Too Old, No Replies

Need function similar to implode()

         

mattfletcher

12:18 am on Aug 30, 2004 (gmt 0)



Hi, I need a function that is similar to implode(), but joins the penultimate and last elements differently.

Four examples:

Joe, John, Jim and Jane
Fred, Frank and Flo
Sally and Sarah
Daniel

Maybe I could use some sort of regex on an imploded string?

A working example would be greatly appreciated.

httpwebwitch

12:55 am on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



O'Reilly php cookbook p.109

[edited by: httpwebwitch at 1:04 am (utc) on Aug. 30, 2004]

jollymcfats

12:56 am on Aug 30, 2004 (gmt 0)

10+ Year Member



Something like this should suit:


function fancy_join($list, $glue=', ', $final_glue=' and ') {
if (count($list)) == 0) return null;
if (count($list)) == 1) return $list[0];
$last = array_pop($list);
return implode($glue, $list) . $final_glue . $last;
}