Created
March 28, 2017 10:14
-
-
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.
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 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 = '&' ) { | |
$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