Skip to content

Instantly share code, notes, and snippets.

@mustardBees
Created March 28, 2017 10:14
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 mustardBees/11cedf7a9b43c20b4d9ca3e1384f3df9 to your computer and use it in GitHub Desktop.
Save mustardBees/11cedf7a9b43c20b4d9ca3e1384f3df9 to your computer and use it in GitHub Desktop.
Useful for creating a comma separated list from array with "&" before the last element.
<?php
/**
* Join a string with a natural language conjunction at the end.
*
* Useful for creating a comma separated list from array with "&" before the
* last element.
*
* @author Daniel James
* @link https://goo.gl/kQK5ke
*
* @param array $list
* @param string $conjunction Optional. Alternative values could be 'and' and 'or'. Default is an ampersand.
*
* @return mixed|string
*/
function iweb_natural_language_join( array $list, $conjunction = '&amp;' ) {
$last = array_pop( $list );
if ( $list ) {
return implode( ', ', $list ) . ' ' . $conjunction . ' ' . $last;
}
return $last;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment