The Oxford Comma
Submitted by joachim.noreiko on Wed, 30/06/2010 - 16:59
Here's a little function I wrote today because I needed to be able to turn a list of between one and three items into a string like 'apples, oranges, and pears', 'apples and pears', or just 'stairs'.
I figured I might as well handle everything in one place, and throw in the option to have an 'or' instead of an 'and'. There may be occasions you don't want the Oxford comma, but I can't think of any.
/**
* Grammatically fun helper to make a list of things in a sentence, ie
* turn an array into a string 'a, b, and c'.
*
* @param $list
* An array of words or items to join.
* @param $type
* The text to use between the last two items. Defaults to 'and'.
* @param $oxford
* Change this from default and you are a philistine.
*/
function oxford_comma_list($list, $type = 'and', $oxford = TRUE) {
$final_join = " $type ";
if ($oxford && count($list) > 2) {
$final_join = ',' . $final_join;
}
$final = array_splice($list, -2, 2);
$final_string = implode($final_join, $final);
array_push($list, $final_string);
return implode(', ', $list);
}Now the real question: how would you make this translatable?


Grammatically fun?!
I like the way you describe this as "grammatically fun" - is there such a thing, or is that an oxymoron? ;)
This is a nice little function and one that deserves to get used, I think, just like Drupal's format_plural().
The translation issue is an interesting one, because I have no idea how these sorts of lists are represented in other languages, or whether grammar has the same meaning, or even importance.
My fav type of comma
My fav type of comma seperated list.
Smelling pisstakes
I think you meant 'separated'.
Text formatters
I think we need a library of text formatters, put it in a module that everyone can then require.
One of my first projects with modifying a module
http://drupal.org/node/331700#comment-1179255
The semi abandoned module based off of this work
http://drupal.org/project/textformatter
thanks
This function is simply beautiful.
Text Formatter module
I just wanted to point out a neat little module that does this with CCK text fields: http://drupal.org/project/textformatter
Ha
LOL, "there's a module for that..." - of course. Love it. In your face, Joachim! ;-)
Double ha -- my use case was
Double ha -- my use case was outside of CCK; it was for messages returned from a form depending on how many checkboxes were selected.
But yes that looks cool.
Post new comment