...

View Full Version : readable_implode() - adds the word "and" before the last array element.



BornKillaz
07-18-2012, 08:55 PM
Hello.

Here's a simple function of mine, similar to implode(), but with one major difference, it adds the word "and" (or any other) before the last element. Useful for comma separated words.



/**
* readable_implode()
*
* @param array $array
* @param string $and
* @return string
*/
function readable_implode( $array, $and = 'and' ) {

$last = array_pop( $array ) . '.';

return ( count( $array ) > 1 ) ? implode( ', ', $array ) . ' ' . $and . ' ' . $last : $last;

}

Try it out...


<?php

$names = array( 'Sandra', 'David', 'Tim', 'Rachel' );

// Should output: Sandra, David, Tim and Rachel.
echo readable_implode( $names );

?>

Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum