Skip to content

Instantly share code, notes, and snippets.

@crishoj
Forked from bluefuton/to_sentence.php
Last active July 14, 2016 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crishoj/dc8d26edb88f59119b131505bd94949d to your computer and use it in GitHub Desktop.
Save crishoj/dc8d26edb88f59119b131505bd94949d to your computer and use it in GitHub Desktop.
Simple implementation of Rails' .to_sentence for PHP
<?php
if (! function_exists('to_sentence')) {
function to_sentence(array $parts, $connector = ', ', $finally = ' and '): string
{
switch (count($parts)) {
case 0:
return '';
case 1:
return reset($parts);
case 2:
return reset($parts) . $finally . end($parts);
default:
return implode($connector, array_slice($parts, 0, -1)) . $finally . end($parts);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment