public static sentence ( array $words, string $twoWordsConnector = ' and ', string $lastWordConnector = null, string $connector = ', ' ) : string | ||
$words | array | the words to be converted into an string |
$twoWordsConnector | string | the string connecting words when there are only two |
$lastWordConnector | string | the string connecting the last two words. If this is null, it will take the value of `$twoWordsConnector`. |
$connector | string | the string connecting words other than those connected by $lastWordConnector and $twoWordsConnector |
return | string | the generated sentence |
public static function sentence(array $words, $twoWordsConnector = ' and ', $lastWordConnector = null, $connector = ', ')
{
if ($lastWordConnector === null) {
$lastWordConnector = $twoWordsConnector;
}
switch (count($words)) {
case 0:
return '';
case 1:
return reset($words);
case 2:
return implode($twoWordsConnector, $words);
default:
return implode($connector, array_slice($words, 0, -1)) . $lastWordConnector . end($words);
}
}