-
-
Save billerickson/0d4f932cbdbfc2a744a8c62349102688 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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