Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Last active August 29, 2015 14:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lavoiesl/eacc7f75663c31135d21 to your computer and use it in GitHub Desktop.
Save lavoiesl/eacc7f75663c31135d21 to your computer and use it in GitHub Desktop.
<?php
function implode_oxford(array $pieces)
{
$pieces = array_values($pieces);
switch (count($pieces)) {
case 0:
return '';
case 1:
return $pieces[0];
case 2:
return $pieces[0].' and '.$pieces[1];
default:
$pieces[count($pieces) - 1] = 'and '.$pieces[count($pieces) - 1];
return implode(', ', $pieces);
}
}
@kherge
Copy link

kherge commented Feb 3, 2015

This sort of thing is fun. 😄

function implode_oxford(array $pieces)
{
    if (count($pieces) > 2) {
        $pieces[] = 'and ' . array_pop($pieces);
    } elseif (count($pieces) > 1) {
        return implode(' and ', $pieces);
    }

    return implode(', ', $pieces);
}

@lavoiesl
Copy link
Author

lavoiesl commented Feb 4, 2015

Yeah, yours is cuter ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment