CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Post a PHP snippet (http://www.codingforums.com/forumdisplay.php?f=41)
-   -   readable_implode() - adds the word "and" before the last array element. (http://www.codingforums.com/showthread.php?t=268216)

BornKillaz 07-18-2012 08:55 PM

readable_implode() - adds the word "and" before the last array element.
 
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.

PHP Code:

/**
 * readable_implode()
 * 
 * @param array $array
 * @param string $and
 * @return string
 */
function readable_implode$array$and 'and' ) {
    
    
$last array_pop$array ) . '.';

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


Try it out...
PHP Code:

<?php

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

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

?>

Thanks.


All times are GMT +1. The time now is 03:04 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.