Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save lavoiesl/eacc7f75663c31135d21 to your computer and use it in GitHub Desktop.

Select an option

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

kherge commented Feb 3, 2015

Copy link
Copy Markdown

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

lavoiesl commented Feb 4, 2015

Copy link
Copy Markdown
Author

Yeah, yours is cuter ;)

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