Created
April 7, 2017 19:17
-
-
Save ryanburnette/e749d810d2da8e36e471cb90d63ede74 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 | |
/* | |
Adapted from: http://stackoverflow.com/a/8586179/2535178 | |
Usage: | |
$items = array('Foo', 'Bar', 'Baz'); | |
echo join_sentence($items).'.'; | |
# Foo, Bar and Baz. | |
*/ | |
function join_sentence($arr) { | |
$comma = ', '; | |
$and = ' and '; | |
$last = array_slice($arr, -1); | |
$first = join($comma, array_slice($arr, 0, -1)); | |
$both = array_filter(array_merge(array($first), $last), 'strlen'); | |
return join($and, $both); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment