Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created October 31, 2016 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/0d4f932cbdbfc2a744a8c62349102688 to your computer and use it in GitHub Desktop.
Save billerickson/0d4f932cbdbfc2a744a8c62349102688 to your computer and use it in GitHub Desktop.
<?php
/**
* Join Multiple Items
* Separate last two items by 'and', remaining by commas
* Ex: item 1, item 2 and item 3
*/
function ea_join_multiple( $items ) {
if ( empty( $items ) ) {
$output = '';
} elseif ( 1 == count( $items ) ) {
$output = $items[0];
} else {
// Combine all but last partial using commas.
$output = implode( ', ', array_slice( $items, 0, -1 ) );
// Add 'and' separator.
$output .= ' and ';
// Add last partial.
$output .= end( $items );
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment