<?php function niceAuthorsPrint(array $authors, $takeCount){ $totalAuthors = count( $authors ); $tailCount = $totalAuthors - $takeCount; $first = array_slice($authors, 0, $takeCount); $othersLabel = $tailCount == 1 ? 'other' : 'others'; $string = implode( ", ", $first ); if($tailCount > 0){ $string .= " and " . $tailCount . ' ' . $othersLabel; } return $string; } // take 3 echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 3) ."\n<br>"; // take 4 echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 4) ."\n<br>"; // take 5 echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 5) ."\n<br>"; ?>
You have javascript disabled. You will not be able to edit any code.